C free and struct

Leandro Galluppi picture Leandro Galluppi · Jul 17, 2011 · Viewed 18.8k times · Source

My question is about C free() function for deallocating memory blocks previously allocated with malloc().
If i have a struct data type compose of several pointers, each of them pointing to different memory locations, what would happen to those memory locations if i apply free() on the struct? will that locations be free too? or just the memory block that allocate the pointer?

Answer

ShinTakezou picture ShinTakezou · Jul 17, 2011

No. They won't be freed. You have to free them "manually". The malloc knows nothing about the content of your struct (it does not know it is a struct at all, it is just a "piece of memory" from its point of view).