C - freeing structs

后端 未结 7 1639
孤独总比滥情好
孤独总比滥情好 2020-11-30 19:43

Let\'s say I have this struct

typedef struct person{
    char firstName[100], surName[51]
} PERSON;

and I am allocating space by malloc and

7条回答
  •  时光说笑
    2020-11-30 20:08

    This way you only need to free the structure because the fields are arrays with static sizes which will be allocated as part of the structure. This is also the reason that the addresses you see match: the array is the first thing in that structure. If you declared the fields as char * you would have to manually malloc and free them as well.

提交回复
热议问题