First element offset

你。 提交于 2019-12-10 21:07:59

问题


Is it a WARRANTY, that offset of first element of structure is 0? To be more accurate, lets consider

struct foo {
int a;
double b;
};
struct foo *ptr=malloc(sizeof(struct foo));
int *int_ptr = &ptr->a;
free(int_ptr)

Is it garantied, that it is valid always, under any os or any other factors?


回答1:


Yes it is guaranteed. Will get you a standard quote, let me lookup.

C99 Standard: §6.7.2.1

Para 12

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.



来源:https://stackoverflow.com/questions/10958576/first-element-offset

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!