Do I need to free each individual element in a struct?
If I have a struct of ints, do I have to individually fr开发者_如何学Goee all of the ints (they are not pointers), or will they be freed when I call free() on the struct?
No, they will be freed when the entire struct is freed.
(Note that you only need to free
a struct that was allocated with malloc
/calloc
/realloc
, not one that was allocated on a stack.)
malloc and free go in pairs.
If you did not allocate memory dynamically for it, don't free it.
精彩评论