开发者

Do I need to free each individual element in a struct?

开发者 https://www.devze.com 2023-02-28 19:17 出处:网络
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 th

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消