"HEAP CORRUPTION DETECTED" [closed]

1

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); }
    
asked by anonymous 05.05.2015 / 11:48

1 answer

0

Your code has few elements of c ++ it is practically using only c. You should try to override the use of arrays by std :: string and also use the class for fstream header file manipulation. This will make your code clearer and with a lower chance of errors.

A modification that would help a little, for example, would seriously change the constructor to the next

#include<string>

Kript::Kript(const std::string & novoTexto)
     :texto(novoTexto)
{
      Enkrip();
} 

Using std :: string you will not have to worry about memory allocation at all times and you also do not have to save the string size, since std :: string knows its own size by the size method (). Take a look at referencing of c / c ++ you could probably do the modifications and the problem of your code will disappear.

    
05.05.2015 / 13:38