C: free(): invalid next size (fast) -
this question has answer here:
hello i've spent last couple hours looking through code attemting find causing memory error. ive been told repeatedly situation of freeing never malloced or freeing twice, have gone through , shrunk code down make sure neither of these possible. whole project linked list, ive taken out of components of project make smallest possible replication of error. thanks! code:
#include <stdlib.h> #include <stdio.h> #include <string.h> #include "list.h" struct lnode{ int count; int line; struct lnode *next; char *word; }; struct lnode* newnode (char* word, int line) { struct lnode *mynode = malloc(sizeof(struct lnode*)); mynode->word = (char*)malloc((strlen(word))*sizeof(char)); strcpy(mynode->word,word); mynode->count = 1; mynode->line = line; mynode->next = null; return mynode; } void freenode(struct lnode * mynode){ //free(mynode->word); //mynode->word = null; free(mynode); mynode = null; } int main(){ struct lnode *n = newnode("test", 2); free(n); return 0; }
gives error:
*** glibc detected *** ./listtest: free(): invalid next size (fast): 0x085d008 ***
change
struct lnode *mynode = malloc(sizeof(struct lnode*));
to
struct lnode *mynode = malloc(sizeof(struct lnode));
and
mynode->word = (char*)malloc((strlen(word))*sizeof(char));
to
mynode->word = (char*)malloc((strlen(word)+1)*sizeof(char));
thank going through effort make smallest test case could.
Comments
Post a Comment