gpt4 book ai didi

c - 第一个元素偏移量

转载 作者:太空狗 更新时间:2023-10-29 16:08:39 25 4
gpt4 key购买 nike

是否有保证,结构的第一个元素的偏移量为 0?为了更准确,让我们考虑一下

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

是否保证它在任何操作系统或任何其他因素下始终有效?

最佳答案

是的,这是有保证的。会给你一个标准报价,让我查一下。

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.

关于c - 第一个元素偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10958576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com