Can anyone help me here? problem in the destructor.
(UP) I think I found the problem, I'll comment on the code:
If someone can give me some good practice tips on this code, please!
Kript::Kript(const char* novoTexto)
{
texto_size = strlen(novoTexto); // * essa linha mudei para (strlen(novoTexto)+1)*
texto = (char*) malloc(sizeof(char)*texto_size);
strcpy(texto, novoTexto); // *pois aqui strcpy copia novoTexto + 'Kript::Kript(const char* novoTexto)
{
texto_size = strlen(novoTexto); // * essa linha mudei para (strlen(novoTexto)+1)*
texto = (char*) malloc(sizeof(char)*texto_size);
strcpy(texto, novoTexto); // *pois aqui strcpy copia novoTexto + '%pre%'*
kripted_txt = (int*)malloc(sizeof(int)*texto_size);
Enkrip();
}
void Kript::Enkrip()
{
file = fopen("KRIPTED.txt", "w");
if (file == NULL)
{
std::cout << "ERRO AO ABRIR ARQUIVO";
}
else
{
int* EOF_POINT = kripted_txt;
for (int i = 0, j = 0; i < texto_size; i++, j = 0)
{
if (j > KEY_SIZE)
j = 0;
kripted_txt[i] = texto[i] * key[j];
EOF_POINT++;
}
EOF_POINT--; //<- adicionado, pois EOF estava sendo adicionado...
//...Fora do espaco alocado
*EOF_POINT = EOF; // agora sim
EOF_POINT = kripted_txt;
while (*EOF_POINT != EOF)
{
fprintf(file, "%d", *EOF_POINT);
EOF_POINT++;
if (*EOF_POINT != EOF)
fprintf(file, " ");
}
}
}
Kript::~Kript()
{
free(texto);
free(kripted_txt);
fclose(file);
}
'*
kripted_txt = (int*)malloc(sizeof(int)*texto_size);
Enkrip();
}
void Kript::Enkrip()
{
file = fopen("KRIPTED.txt", "w");
if (file == NULL)
{
std::cout << "ERRO AO ABRIR ARQUIVO";
}
else
{
int* EOF_POINT = kripted_txt;
for (int i = 0, j = 0; i < texto_size; i++, j = 0)
{
if (j > KEY_SIZE)
j = 0;
kripted_txt[i] = texto[i] * key[j];
EOF_POINT++;
}
EOF_POINT--; //<- adicionado, pois EOF estava sendo adicionado...
//...Fora do espaco alocado
*EOF_POINT = EOF; // agora sim
EOF_POINT = kripted_txt;
while (*EOF_POINT != EOF)
{
fprintf(file, "%d", *EOF_POINT);
EOF_POINT++;
if (*EOF_POINT != EOF)
fprintf(file, " ");
}
}
}
Kript::~Kript()
{
free(texto);
free(kripted_txt);
fclose(file);
}