I'm trying to troubleshoot a c++ problem where two parts of my code are returning different results for the sizeof() operator.
Here's what I run
MyClass* foo = new MyClass();
int size = sizeof(*foo)
I place this code in two different sections of my project and I get two different results. One time it 2254, another is 2284. I can look at the memory layout and one area shows the internal members as byte-aligned, another area it's word aligned.
I look at the dissasmbly and see that the sizeof() values are actually part of the machine code. Would this be a bug in the compiler or the linker? Why would two parts of the same project view the same class differently?
EDIT:
Let me provide a more clear example that I just ran to show that this is NOT a ODR violation.
I just made a brand new class as such
class TestAlignClass
{
public:
TestAlignClass() { }
~TestAlignClass() { }
private:
char charArray[3];
int myInt;
};
If the class is aligned to 4, it should return sizeof() = 8 which is what I want. But there are certain classes in my code that return sizeof() = 7.
In fact when I step into the new() operator, sometimes it allocates 7 bytes and sometimes it allocate 8.
I am linking several projects together and I thought it had to do with project settings at first, but different parts of the same project will show the discrepancy.