By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

sizeof of an instance differ from sizeof of a class

P: n/a
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

Nov 2 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Nov 3 '06 #2

P: n/a

GRenard wrote:
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.

Nov 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.