数组结构,内存是连续的吗?
如果我除了
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,如果我们忽略
a
之间可能的填充,a
、b
和c
将是连续的code> 和b
,或b
和c
。当然,b
的元素之间没有填充。Yes,
a
, (the elements of)b
, andc
will be contiguous, if we ignore possible padding betweena
andb
, orb
andc
. Of course there's no padding between the elements ofb
.