sizeof of an instance differ from sizeof of a class

G

GRenard

Hello,

We just switch in our company to VisualStudio 2005 and the new ATL
library.
We use a wrapper to use CFileDialog. Its name is CFileDialogDeluxe

Here the call
CFileDialogDeluxe oFileDialog( ... );

There are some problems in the debug, look the sizeof from the debug :

sizeof(oFileDialog) 1144 unsigned int
sizeof(CFileDialogDeluxe) 1156 unsigned int
sizeof((*(WTL::CFileDialog*)(&oFileDialog))) 1156 unsigned int

Now the CFileDialogDeluxe just call the constructor of CFileDialog, I
removed all the members from our class.
But when I put members, there are some offsets on the address which
make the program crashes when destructing variables.

So what can affect the sizeof of a variable like that because it
inserts offset of variables.


Thank you very much

Jean-Sébastien Goupil
 
D

David W

Possibly you are compiling different files with different structure/class member packing. For some
compilers, members can be aligned to reduce memory accesses by the CPU or they can be packed
together as tightly as possible, depending on compiler options chosen. If the options differ between
source files, you'll get a mismatch in member location between different parts of your program. I
have had programs crash for this reason. Look up your compiler's documemtation for member packing
options..

DW
 
S

Salt_Peter

GRenard said:
Hello,

We just switch in our company to VisualStudio 2005 and the new ATL
library.
We use a wrapper to use CFileDialog. Its name is CFileDialogDeluxe

Here the call
CFileDialogDeluxe oFileDialog( ... );

There are some problems in the debug, look the sizeof from the debug :

sizeof(oFileDialog) 1144 unsigned int
sizeof(CFileDialogDeluxe) 1156 unsigned int
sizeof((*(WTL::CFileDialog*)(&oFileDialog))) 1156 unsigned int

Now the CFileDialogDeluxe just call the constructor of CFileDialog, I
removed all the members from our class.
But when I put members, there are some offsets on the address which
make the program crashes when destructing variables.

So what can affect the sizeof of a variable like that because it
inserts offset of variables.


Thank you very much

Jean-Sébastien Goupil

Padding is causing the offsets to change. Padding should never affect
the destruction of members. If it does, you've got undefined behaviour.
If you are allocating/deallocating members through pointers based on
the sizeof involved and their "offsets", you have undefined behaviour.
That is strictly forbidden in C++. Padding can change depending on
platform and compiler switches. A properly designed program remains
unaffected by these options.

Lets take a silly example:
#include <iostream>

struct A
{
int n;
char c;
};

int main()
{
A a;
std::cout << "sizeof(int) = " << sizeof(int) << std::endl;
std::cout << "sizeof(char) = " << sizeof(char) << std::endl;
std::cout << "sizeof(A) = " << sizeof(A) << std::endl;
}

/*
sizeof(int) = 4
sizeof(char) = 1
sizeof(A) = 8
*/

This is expected. The compiler on this particular platform padded the
class to match this computer's memory architecture.
If i had newed/malloc the instances of the class and/or members, its my
responsability to deallocate those objects and/or members via pointers
that are unaffected by the padding involved. Thats the law.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top