Visual C++ 2008 variables overlap in memory

276 Views Asked by At

I've got a strange problem. I've got a program where I'm using some static variables (some of which are objects including other variables) and some of them seem to overlap in memory in _DEBUG_ mode. The program is rather complex and I've not yet had time to reproduce it in a smaller use case. So it looks like this:

struct Bar  
{ 
   int i;
... 
};

class A 
{ 
public:
 Bar b;
... 
};

class B
{ 
public:
 static FOO f;
... 
 static A a;
};

I then set a break point at _tmainCRTStartup in order to see what happens _BEFORE_ any of my code was ran. Once the break point is hit, I'm looking at the variables and their addresses in the Watch window and see the following picture:

&B::f - 0x00fa68e0

&(B::a.b) - 0x00fa68cc 

sizeof(B::a.b) - 28

But (0x00fa68e0 - 0x00fa68cc) = 20

So, these variables overlap in memory (if I change "i", another object will be affected). I started looking at the map file an B::a is defined at 0x00fa6340.

So is this a linker or compiler bug? No code has ran yet, yet there is a memory overlap already here.

Have you come across this in VS 2008?

Is this some known bug, is there a workaround or am I missing something in my code?

0

There are 0 best solutions below