473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

sizeof of an instance differ from sizeof of a class

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
2 2570
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: balasubramaniam | last post by:
I comile the code in unix where sizof(int)=4 #include<iostream> using namespace std; class A { int i; static int j; }; int A::j = 10;
19
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As...
12
by: news.fe.internet.bosch.com | last post by:
Hi All , I u find out size of struct , does it considers paddding chars into consideration struct A { char c; int i; };
4
by: Mike | last post by:
This question has been in the back of my head for some time, so I'll ask. If I have a method that has 200 Lines of code and I create 1000 instances of the class to which the method belongs, are...
1
by: nicolas.gendron | last post by:
Is there a way to do this? I don't want to use sizeof(non_static_member_variable_type) because if the type of my member variable changes, the sizeof operator won't be informe. Any solution? ...
32
by: moleskyca1 | last post by:
This may be stupid question, but why is sizeof(Base) == 1 in: int main(int argc, char* argv) { class Base { }; cout << sizeof(Base) << endl; return 0; }
5
by: mathemagic | last post by:
Hello All, I apologise if this has been covered by an earlier post. I just wanted to know how the functions of a class are tied to the class. ie, in C structs, since u can have only data...
27
by: CodeMonk3y | last post by:
gotta question on sizeof keyword does the sizeof keyword calcuates the size at compile time or run time ?? -- Posted on news://freenews.netfront.net - Complaints to news@netfront.net --
45
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.