假设我有这个 struct
typedef struct person{
char firstName[100], surName[51]
} PERSON;
我通过malloc分配空间,并用一些值填充它
PERSON *testPerson = (PERSON*) malloc(sizeof(PERSON));
strcpy(testPerson->firstName, "Jack");
strcpy(testPerson->surName, "Daniels");
释放该 struct 占用的所有内存的正确且安全的方法是什么?是"免费(测试人员);"够了,还是需要逐个释放每个 struct 的属性?
这就引出了另一个问题-- struct 是如何存储在内存中的?我注意到一个奇怪的行为-当我试图打印 struct 地址时,它等于第一个属性的地址.
printf("Structure address %d == firstName address %d", testPerson, testPerson->firstName);
Which means that this free(testPerson) should be equal to this free(testPerson->firstName);
这不是我想做的.
谢谢