I have a structure which contains some elements, i free the memory of this structure in a loop, roughly like:
for (i = 0; i < teller; i++) {
free((glycan+i)->desc);
}
free(glycan)
I assume that the pointers are still pointing to the empty memory blocks, therefore i wanted to set them to NULL as follows:
for (i = teller; i > 0; i--) {
(glycan+i)->desc = NULL;
}
glycan = NULL;
Valgrind however tells me something which i don't really understand:
==11783== Invalid write of size 4
==11783== at 0x8048F49: main (spectral_matcher.c:122)
==11783== Address 0x431c070 is 72 bytes inside a block of size 28,000 free'd
==11783== at 0x4027C02: free (vg_replace_malloc.c:366)
==11783== by 0x8048F2C: main (spectral_matcher.c:121)
Can anyone explain to me why this warning/error occurs and what i should do differently not to solve it?
EDIT: I am aware that i am setting the pointer to NULL after freeing, freeing only marks the memory as free so the pointer is still intact (if i'm not mistaking) which i subsequently wish to set to NULL.