【发布时间】:2010-07-14 23:26:06
【问题描述】:
所以我在教我的朋友指针。这样做时,我注意到两个相同结构的地址完全是背靠背的。
struct Foo
{
int a;
};
struct Bar
{
int b;
};
这让我可以这样做:
Foo foo; foo.a = 100;
Bar bar; bar.b = 100;
Foo *pFoo = &foo;
Bar *pBar = &bar;
(pFoo+1)->a = 200;
这会覆盖 bar.b 中的值并将其设置为 200。
现在,我并没有质疑这样做的好处——它可能永远不会在真正的程序中出现。我只是想知道,操作系统是否总是背靠背分配相同的结构?前提是给定区域中有足够的可用内存空间。
【问题讨论】: