使用指向第一个成员的指针调用 free 是否有效?
Is it vali to call free with a pointer to the first member?
可以在指向结构第一个成员的指针上调用 free 吗(结构是与 malloc 相关的结构)?我知道原则上指针无论如何都指向正确的东西......
struct s {int x;};
//in main
struct s* test;
test = (struct s*) malloc(sizeof(*test));
int* test2;
test2 = &(test->x);
free(test2); //is this okay??
还有,如果int x
换成struct,答案会不会改变?
更新:我为什么要写这样的代码?
struct s {int x;};
struct sx1 {struct s test; int y;}; //extending struct s
struct sx2 {struct s test; int z;}; //another
// ** some functions to keep track of the number of references to each variable of type struct s
int release(struct s* ptr){
//if the number of references to *ptr is 0 call free on ptr
}
int main(){
struct sx1* test1;
struct sx2* test2;
test1 = (sx1*) malloc(sizeof(*sx1));
test2 = (sx2*) malloc(sizeof(*sx2));
//code that changes the number of references to test1 and test2, calling functions defined in **
release(test1);
release(test2);
}
根据 C11 标准,章节 §6.7.2.1
[...] There may be unnamed
padding within a structure object, but not at its beginning.
这意味着结构的开头不能有任何填充。因此,第一个成员将具有与结构变量相同的地址。
free()
需要一个之前由 malloc()
或家人返回的指针。
在您的例子中,您传递的地址与 malloc()
返回的地址相同。所以,你很高兴。
是的,没关系。
6.7.2.1
- Within a structure object, the non-bit-field members and the units in which bit-fields
reside have addresses that increase in the order in which they are declared. A pointer to a
structure object, suitably converted, points to its initial member (or if that member is a
bit-field, then to the unit in which it resides), and vice versa. There may be unnamed
padding within a structure object, but not at its beginning.
也就是说这样定义的:
struct s {int x;};
struct s* test;
test = (struct s*) malloc(sizeof(*test));
int* p = &(test->x);
free(p);
可以在指向结构第一个成员的指针上调用 free 吗(结构是与 malloc 相关的结构)?我知道原则上指针无论如何都指向正确的东西......
struct s {int x;};
//in main
struct s* test;
test = (struct s*) malloc(sizeof(*test));
int* test2;
test2 = &(test->x);
free(test2); //is this okay??
还有,如果int x
换成struct,答案会不会改变?
更新:我为什么要写这样的代码?
struct s {int x;};
struct sx1 {struct s test; int y;}; //extending struct s
struct sx2 {struct s test; int z;}; //another
// ** some functions to keep track of the number of references to each variable of type struct s
int release(struct s* ptr){
//if the number of references to *ptr is 0 call free on ptr
}
int main(){
struct sx1* test1;
struct sx2* test2;
test1 = (sx1*) malloc(sizeof(*sx1));
test2 = (sx2*) malloc(sizeof(*sx2));
//code that changes the number of references to test1 and test2, calling functions defined in **
release(test1);
release(test2);
}
根据 C11 标准,章节 §6.7.2.1
[...] There may be unnamed padding within a structure object, but not at its beginning.
这意味着结构的开头不能有任何填充。因此,第一个成员将具有与结构变量相同的地址。
free()
需要一个之前由 malloc()
或家人返回的指针。
在您的例子中,您传递的地址与 malloc()
返回的地址相同。所以,你很高兴。
是的,没关系。
6.7.2.1
- Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning.
也就是说这样定义的:
struct s {int x;};
struct s* test;
test = (struct s*) malloc(sizeof(*test));
int* p = &(test->x);
free(p);