Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sizeof of an instance differ from sizeof of a class

Reply
Thread Tools

sizeof of an instance differ from sizeof of a class

 
 
GRenard
Guest
Posts: n/a
 
      11-02-2006
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

 
Reply With Quote
 
 
 
 
David W
Guest
Posts: n/a
 
      11-03-2006
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


 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      11-03-2006

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Don't understand behavior; instance form a class in another class'instance Martin P. Hellwig Python 1 03-26-2010 12:06 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
sizeof(Class)/sizeof(Object) Nikolai Weibull Ruby 2 12-31-2004 06:42 PM
converting base class instance to derived class instance Sridhar R Python 14 02-10-2004 02:47 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments