1
Can anyone help me out here? Destructor problem.
(UP) I think I found the problem, I will comment on the code:
If anyone can give me some tips on good practice in 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 + '\0'*
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);
}
Your code is C++ but you still use malloc and char*? Why not use streams and strings to solve the problem?
– bruno
You’re right, alias already changed from malloc and free, to new and delete, but I haven’t practiced the use of Std::string,
– Rafael Moura