c - How to free a calloc'd pointer without removing value from hashtable/linked list? -


i'm setting , loading hashtable, using linked lists. each item, calloc struct pointer, enter item array itself, or associated linked list if array index taken.

all works beautifully, valgrind telling me have "definitely lost" @ line calloc'ing struct item.

now, i'm new c, familiar objective-c. in pre-arc days, alloc object, put in array, release it. array retain object. once needed pull item out of array, need retain first. otherwise, lost array no longer retaining object.

so, guess question is: how array/linked list retain "objects", can free alloc'd item after entering array or linked list?

// global array struct_item** hashtable;  // declare struct typedef struct struct_item {     char item1[size + 1];     struct struct_item* next_item; }  // later... dynamically allocate hashtable hashtable = calloc(sizevar, sizeof(struct_item);  // later, inside of loop... // alloc struct item go array/linked list (valgrind error happening here) struct_item* an_item = calloc(1, sizeof(struct_item);  // after setting appropriate values in 'an_item', add struct hashtable hashtable[0] = an_item;  // after said , done, free current (remaining) struct_item // (edit fix: wasn't struct_item; should've been an_item) //free(struct_item); free(an_item); 

i'm not freeing of other struct_items calloc'd during loop; last one. but, free one, it's value disappears hashtable.

ideally, suppose i'd freeing each 1 of calloc'd struct_items, yes? valgrind reporting 1 issue, located @ line i'm calloc'ing 'an_item'.

so, again, how calloc'd items stay associated hashtable, can free 'an_item' without it's data disappearing table? missing???

edit add note: free struct_items when i'm unloading hashtable/linked list!

found it!

i freeing linked list during unload - not actual item @ current hashtable index!

once freed hashtable[i] correctly, got happy report:

==6759==  ==6759== heap summary: ==6759==     in use @ exit: 0 bytes in 0 blocks ==6759==   total heap usage: 143,130 allocs, 143,130 frees, 14,883,824 bytes allocated ==6759==  ==6759== heap blocks freed -- no leaks possible ==6759==  ==6759== counts of detected , suppressed errors, rerun with: -v ==6759== error summary: 0 errors 0 contexts (suppressed: 0 0) 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -