c - Program crashing using malloc() and free() -


i appear crashing program when trying free memory sections. following overall structure of linked lists:

typedef struct {     char                            *dataitem;     struct listelement              *link;     int16_t                         wordsize;     int16_t                         (*libword)[q];     char                            gpiovalue;     struct listelement              *syllables; }listelement; 

the program crashes when calling function:

recordedwordspointer = removeitem(recordedwordspointer);                            // rid of junk stored in recorded buffer 

where:

volatile listelement *recordedwordspointer; 

has stored values in libword , points next link if there another, otherwise null. following shows happens when entering function:

listelement * removeitem (listelement * listpointer) {     cpu_irq_disable();     listelement * tempp = listpointer;      while( listpointer->syllables != null ){         removesyllable(listpointer->syllables);     }     if( listpointer != null ){         tempp = listpointer -> link;         free (listpointer->dataitem);         free (listpointer->libword);         free (listpointer);     }     cpu_irq_enable();        return tempp; }  void removesyllable (listelement * listpointer) {      while( listpointer->syllables != null ){         removesyllable(listpointer->syllables);     }     free (listpointer->dataitem);     free (listpointer->libword);     free (listpointer);     listpointer = null;     return; } 

i wondering if doing wrong cause memory crash?

thanks!

edit:

i asked show how construct memory locations help. use following 2 functions:

listelement * additem (listelement * listpointer, char* name, int16_t size, int16_t wordlength, int16_t (*words)[q]) {     // returns listpointer @ beginning of list     listelement * lp = listpointer;     listelement * listpointertemp;     char errorhandler = null;     // @ end of list?     if (listpointer != null) {         // move down end of list         while (listpointer -> link != null)         listpointer = listpointer -> link;         listpointertemp = listpointer;         listpointer -> link = (struct listelement  *) malloc (sizeof (listelement));         // on fail end links becomes null above         if(listpointer -> link != null){             listpointer = listpointer -> link;             listpointer -> link = null;             listpointer -> wordsize = wordlength;             listpointer -> syllables = null;              listpointer -> dataitem = (char*) malloc ((size + 1)*sizeof(char));             if(listpointer -> dataitem != null){                 for(int i=0; i<size ; i++){                     listpointer -> dataitem[i] = name[i];                 }                 listpointer -> dataitem[size] = null;                  listpointer -> libword =  (int16_t(*)[q])malloc(wordlength*q*sizeof(int16_t));                 if(listpointer -> libword != null){                     (int16_t row=0 ; row < wordlength ; row++){                         (int col=0 ; col < q ; col++){                             listpointer -> libword[row][col]  = words[row][col];                         }                     }                     errorhandler = 1;                 }else{                     free(listpointer->dataitem);                     free(listpointer);                     listpointertemp -> link = null;                 }             }else{                 free(listpointer);                 listpointertemp -> link = null;             }         }         if(errorhandler == null){             //failure             usart_write_line(&avr32_usart0,"\r\n--------------------------------------------\r\n");             usart_write_line(&avr32_usart0,"ran out of memory!  word not created.\r\n");             usart_write_line(&avr32_usart0,"\r\n--------------------------------------------\r\n");         }         return lp;     } else {         listpointer = (struct listelement  *) malloc (sizeof (listelement));          if(listpointer != null){             listpointer -> link = null;             listpointer -> wordsize = wordlength;             listpointer -> syllables = null;              listpointer -> dataitem = (char*) malloc ((size + 1)*sizeof(char));             if(listpointer -> dataitem != null){                 for(int16_t i=0; i<size ; i++){                     listpointer -> dataitem[i] = name[i];                 }                 listpointer -> dataitem[size] = null;                  listpointer -> libword =  (int16_t(*)[q])malloc(wordlength*q*sizeof(int16_t));                 if(listpointer -> libword != null){                     (int16_t row=0 ; row < wordlength ; row++){                         (int col=0 ; col < q ; col++){                             listpointer -> libword[row][col]  = words[row][col];                         }                     }                     errorhandler = 1;                 }else{                     free(listpointer->dataitem);                     free(listpointer);                     listpointertemp -> link = null;                 }             }else{                 free(listpointer);                 listpointertemp -> link = null;             }         }         if(errorhandler == null){             //failure             usart_write_line(&avr32_usart0,"\r\n--------------------------------------------\r\n");             usart_write_line(&avr32_usart0,"ran out of memory!  word not created.\r\n");             usart_write_line(&avr32_usart0,"\r\n--------------------------------------------\r\n");         }         return listpointer;     } }  listelement* addsyllable (listelement * listpointer, char* name, int16_t size, int16_t wordlength, int16_t (*words)[q]) {     // returns listpointer @ beginning of list     listelement * lp = listpointer;     listelement * listpointertemp;     char errorhandler = null;     // @ end of list?     if (listpointer != null) {         // move down end of list         while (listpointer -> syllables != null)         listpointer = listpointer -> syllables;         listpointertemp = listpointer;         listpointer -> syllables = (struct listelement  *) malloc (sizeof (listelement));         // on fail end links becomes null above         if(listpointer -> syllables != null){             listpointer = listpointer -> syllables;             listpointer -> link = null;             listpointer -> wordsize = wordlength;              listpointer -> dataitem = (char*) malloc ((size + 1)*sizeof(char));             if(listpointer -> dataitem != null){                 for(int i=0; i<size ; i++){                     listpointer -> dataitem[i] = name[i];                 }                 listpointer -> dataitem[size] = null;                  listpointer -> libword =  (int16_t(*)[q])malloc(wordlength*q*sizeof(int16_t));                 if(listpointer -> libword != null){                     (int16_t row=0 ; row < wordlength ; row++){                         (int col=0 ; col < q ; col++){                             listpointer -> libword[row][col]  = words[row][col];                         }                     }                     errorhandler = 1;                 }else{                     free(listpointer->dataitem);                     free(listpointer);                     listpointertemp -> syllables = null;                 }             }else{                 free(listpointer);                 listpointertemp -> syllables = null;             }         }         if(errorhandler == null){             //failure             usart_write_line(&avr32_usart0,"\r\n--------------------------------------------\r\n");             usart_write_line(&avr32_usart0,"ran out of memory!  word not created.\r\n");             usart_write_line(&avr32_usart0,"\r\n--------------------------------------------\r\n");         }         return lp;     } else {         listpointer = (struct listelement  *) malloc (sizeof (listelement));          if(listpointer != null){             listpointer -> link = null;             listpointer -> wordsize = wordlength;              listpointer -> dataitem = (char*) malloc ((size + 1)*sizeof(char));             if(listpointer -> dataitem != null){                 for(int16_t i=0; i<size ; i++){                     listpointer -> dataitem[i] = name[i];                 }                 listpointer -> dataitem[size] = null;                  listpointer -> libword =  (int16_t(*)[q])malloc(wordlength*q*sizeof(int16_t));                 if(listpointer -> libword != null){                     (int16_t row=0 ; row < wordlength ; row++){                         (int col=0 ; col < q ; col++){                             listpointer -> libword[row][col]  = words[row][col];                         }                     }                     errorhandler = 1;                 }else{                     free(listpointer->dataitem);                     free(listpointer);                     listpointertemp -> syllables = null;                 }             }else{                 free(listpointer);                 listpointertemp -> syllables = null;             }         }         if(errorhandler == null){             //failure             usart_write_line(&avr32_usart0,"\r\n--------------------------------------------\r\n");             usart_write_line(&avr32_usart0,"ran out of memory!  word not created.\r\n");             usart_write_line(&avr32_usart0,"\r\n--------------------------------------------\r\n");         }         return listpointer;     } } 

your removesyllable function doesn't set syllables member null. think does, inside routine, changing value inside local variable.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -