C - freeing structs

后端 未结 7 1594
孤独总比滥情好
孤独总比滥情好 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:13

    Because you defined the struct as consisting of char arrays, the two strings are the structure and freeing the struct is sufficient, nor is there a way to free the struct but keep the arrays. For that case you would want to do something like struct { char *firstName, *lastName; }, but then you need to allocate memory for the names separately and handle the question of when to free that memory.

    Aside: Is there a reason you want to keep the names after the struct has been freed?

    0 讨论(0)
提交回复
热议问题