数组结构,内存是连续的吗?

发布于 2025-01-11 07:15:25 字数 315 浏览 2 评论 0原文

如果我除了

struct S {
    int a;
    float* b;
    int c;
};

任何填充之外。 a、b(保存指针的变量)和c 将是连续的。 b 指向的元素可能位于内存中的其他位置,

如果我有

struct S {
    int a;
    float b[10];
    int c;
};

a,则 b 和 c 的每个元素在内存中都是连续的。正确的? 我编写了一个测试程序并查看了地址进行确认,但我不确定编译器是否有帮助或者是否有保证。

if I have

struct S {
    int a;
    float* b;
    int c;
};

Aside from any padding. a, b (the variable where a pointer is kept), and c will be contiguous.
The element that b is pointing to, may be somewhere else in memory

if I have

struct S {
    int a;
    float b[10];
    int c;
};

a, every element of b, and c will all be contiguous in memory. Correct?
I wrote a test program and looked at the addresses to confirm, but I am not sure if that is the compiler being helpful or it is guaranteed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渔村楼浪 2025-01-18 07:15:25

是的,如果我们忽略 a 之间可能的填充,abc 将是连续的code> 和 b,或 bc。当然,b 的元素之间没有填充。

Yes, a, (the elements of) b, and c will be contiguous, if we ignore possible padding between a and b, or b and c. Of course there's no padding between the elements of b.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文