HEAP CORRUPTION DETECTED in C -


i having problems program , getting error : heap corruption detected: before normal block (#9873672) @ 0x00968988. crt detected application wrote memory before start of heap buffer.

i have tried fixes can't figure out wrong program, fix , :( here function i'm using , causing me problems : doing file specific keyword (argument of function gettext) , printing matching value. sorry if of variables in french, it's project school , our teacher require use french names >_<

#include "gettext.h" #include "main.h" #include <stdlib.h>  textelangue* ressourcestextelangue = null; int compteur = 0;   char* gettext(char* clef) {     char* texte = null;     texte = clef; //clef keyword passed in function argument     textelangue temp;     temp.clef = clef;     textelangue* resultat = (textelangue*) bsearch(&temp, ressourcestextelangue, compteur, sizeof(textelangue), comparerclef); //returns value associated key      if (clef != null)     {             if (resultat != null)             texte = resultat->valeur;     }     return texte;  }  void lecturetexte(char* langue) {     char nomfichierressources[64];     sprintf(nomfichierressources, "ressources_%s.txt", langue); //give file name specific ending depending on language chosen     file* fichierressources = fopen(nomfichierressources, "r");     if (fichierressources == null)     {         system("cls");         perror("the following error occured ");         system("pause");         exit(42);     }          //allocates memory language resources      int taille = 10;     ressourcestextelangue = (textelangue *) calloc(taille, sizeof(textelangue));     if (ressourcestextelangue == null)         printf("pas assez de place mémoire pour les ressources texte");           //gives value textresource.key , textresource.value each line of file     char* ligne;     while ((ligne = lectureligne(fichierressources)))     {         if (strlen(ligne) > 0)         {             if (compteur == taille)             {                 taille += 10;                 ressourcestextelangue = (textelangue *) realloc(ressourcestextelangue, taille * sizeof(textelangue));             }             ressourcestextelangue[compteur].clef = ligne;              while (*ligne != '=')             {                 ligne++;             }              *ligne = '\0';             ligne++;             ressourcestextelangue[compteur].valeur = ligne;             compteur++;         }      }          //sorts out values of textresource obtained      qsort(ressourcestextelangue, compteur, sizeof(textelangue), comparerclef);       fclose(fichierressources); }   //reads line , returns  char* lectureligne(file *fichier)  {     int longeur = 10, = 0, c = 0;     char* ligne = (char*) calloc(longeur, sizeof(char));      if (fichier)     {         c = fgetc(fichier);         while (c != eof)         {             if (i == longeur)             {                 longeur += 10;                 ligne = (char*) realloc(ligne, longeur * sizeof(char));             }             ligne[i++] = c;             c = fgetc(fichier);             if ((c == '\n') || (c == '\r'))                 break;         }          ligne[i] = '\0';          while ((c == '\n') || (c == '\r'))             c = fgetc(fichier);         if (c != eof)             ungetc(c,fichier);          if ((strlen(ligne) == 0) && (c == eof))         {             free(ligne);             ligne = null;         }     }     return ligne; }  //frees textressources void liberertexte() {     if (ressourcestextelangue != null)     {         while (compteur--)         {             free(ressourcestextelangue[compteur].clef);          }         free(ressourcestextelangue);     } }  //compares keys int comparerclef(const void* e1, const void* e2) {     return strcmp(((textelangue*) e1)->clef, ((textelangue*) e2)->clef); } 

the structure of ressourcetextelangue (textresources) :

typedef struct textelangue {     char* clef;     char* valeur; } textelangue; 

there several potential problems code causing error report see.

here one:

    if (i == longeur) 

should be:

    if ((i+1) == longeur) 

otherwise,

    ligne[i] = '\0'; 

can occur in conditions when

        ligne[i++] = c; 

has caused i become equal longeur.

here another:

        while (*ligne != '=')         {             ligne++;         }          *ligne = '\0'; 

the above code should be:

        while (*ligne != '=' && *ligne != '\0')         {             ligne++;         }          *ligne = '\0'; 

otherwise, corrupt memory in case when there no '=' found in string.

although either of these cause symptom report, see other oddities make me think there more wrong have seen far. nevertheless, fixing 2 problems @ least reduce number of possibilities have consider.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -