第一个元素偏移 [英] First element offset
问题描述
这是个保证,结构的第一个元素是0的偏移?
为了更准确,让我们考虑
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)
难道garantied,它是有效的始终,任何操作系统或任何其他因素下?
Is it garantied, that it is valid always, under any os or any other factors?
推荐答案
是的,它是保证。将让你一个标准的报价,让我查找。
Yes it is guaranteed. Will get you a standard quote, let me lookup.
C99标准:§6.7.2.1
第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.
这篇关于第一个元素偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!