So I was teaching my friend about pointers. While doing so, I noticed that the address of two identical structs are exactly back-to-back.
struct Foo
{
int a;
};
struct Bar
{
int b;
};
Which allowed me to do this:
Foo foo; foo.a = 100;
Bar bar; bar.b = 100;
Foo *pFoo = &foo;
Bar *pBar = &bar;
(pFoo+1)->a = 200;
This overrides the value in bar.b and sets it to 200.
Now, I'm not questioning the merits of doing such a thing- it will probably never see the light of day in a real program. I was just wondering, does the OS always allocate identical structs back-to-back? Provided there is enough memory space free in the given area.