c - Freeing Allocated Memory From A Struct -
i sending struct food function thats job free allocated memory of struct not struct itself.
the struct looks this:
struct food{ char * name; char * foodgroup; double calories; char type; struct food * next; };
the char *name , char *foodgroup memory allocated in function.
struct food* temp = malloc(sizeof(struct food)); temp->name = malloc(sizeof(temp->name)); /*error?*/ temp->foodgroup = malloc(sizeof(temp->name)); /*error?*/ temp->next = malloc(sizeof(struct food));
the function frees allocated memory looks this:
void destroyelement(food *theelement) { free(theelement->name); /*error*/ free(theelement->foodgroup); /*error*/ }
i used print statements determine location of segmentation fault , looks in destroyelement
function.
instead of malloc(sizeof(temp->name))
, try malloc(sizeof(thing want put in temp->name)
.
it seems trying take sizeof(temp->name)
, thing trying malloc in first place (if temp->name
being malloced right now, it's size?)
does make sense?
if not, feel free email judi, or ask on bucky. ;)
Comments
Post a Comment