Home > Archive > VC Language > November 2005 > sizeof(class x)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Thomas Swezey 2005-11-28, 7:59 am |
| If I use the sizeof operator to get the size of a class, what size does it
return?
Example:
class x {
int i1;
double d1;
void func1(void);
int func2(int iX);
};
int iSize;
iSize = sizeof(class x);
What is iSize going to be? Just data? Just data and vTab pointers?
Surely not including code!
Is there a C++ definition for this I can count on?
Thanks a lot in advance,
--Tom Swezey
--
**** Signature **** (KISS: Keep It Simple, STUPID!) ****
Check out my Web Site: http://www.winternet.com/~swezeyt
(o o) E-mail me at swezeyt@winternet.com
oOO*( )*OOo***********************************
*
| |
|
| It will be the size of the data only, but note that due to alignment, the
actual size can be bigger than the sum of the sizes of each data member. If
the class has virtual methods then the data also includes a VMT pointer.
--
Best regards
Mark
"Thomas Swezey" <swezeyt@winternet.com> wrote in message
news:OvVyFCB9FHA.2576@TK2MSFTNGP12.phx.gbl...
> If I use the sizeof operator to get the size of a class, what size does it
> return?
>
> Example:
>
> class x {
> int i1;
> double d1;
> void func1(void);
> int func2(int iX);
> };
>
> int iSize;
> iSize = sizeof(class x);
>
> What is iSize going to be? Just data? Just data and vTab pointers?
> Surely not including code!
>
> Is there a C++ definition for this I can count on?
>
> Thanks a lot in advance,
> --Tom Swezey
> --
> **** Signature **** (KISS: Keep It Simple, STUPID!) ****
> Check out my Web Site: http://www.winternet.com/~swezeyt
> (o o) E-mail me at swezeyt@winternet.com
> oOO*( )*OOo***********************************
*
>
| |
| Alex Blekhman 2005-11-28, 7:59 am |
| Thomas Swezey wrote:
> If I use the sizeof operator to get the size of a class,
> what size does it return?
>
> Example:
>
> class x {
> int i1;
> double d1;
> void func1(void);
> int func2(int iX);
> };
>
> int iSize;
> iSize = sizeof(class x);
>
> What is iSize going to be? Just data? Just data and vTab
> pointers? Surely not including code!
>
> Is there a C++ definition for this I can count on?
C++ Standard, 5.3.3/2:
"[...] When applied to a class, the result is the number of
bytes in an object of that class including any padding
required for placing objects of that type in an array."
If class has virtual functions, then its size grows for size
of pointer (to vtable). It's true for MSVC compiler and
probably many others. However, it's implementation specific
details and you should not count on it.
If class has no members, then its size is usually is 1.
| |
| John Carson 2005-11-28, 7:59 am |
| "Thomas Swezey" <swezeyt@winternet.com> wrote in message
news:OvVyFCB9FHA.2576@TK2MSFTNGP12.phx.gbl
> If I use the sizeof operator to get the size of a class, what size
> does it return?
>
> Example:
>
> class x {
> int i1;
> double d1;
> void func1(void);
> int func2(int iX);
> };
>
> int iSize;
> iSize = sizeof(class x);
>
> What is iSize going to be? Just data? Just data and vTab pointers?
> Surely not including code!
> Is there a C++ definition for this I can count on?
It is everything required per object, plus possible padding for alignment
reasons. Code for functions is per class, not per object, so it is excluded
along with static data. Member data, virtual function table pointers (if
applicable) and virtual base table pointers (used in conjuction with virtual
inheritance) are all included --- at least with VC++. The following should
give you an idea:
#include <iostream>
using namespace std;
class Simple
{
int x;
int foo()
{
char str[] = "Really long, long, long string\n";
cout << str;
}
};
class A
{
int x;
public:
virtual int foo(){}
};
class B: virtual public A
{
public:
virtual int foo(){}
};
int main()
{
int s = sizeof(Simple); // 4
int a = sizeof(A); // 8
int b = sizeof(B); // 12
return 0;
}
--
John Carson
| |
|
| Just to complete, in Vc, you can fix/adjust alignment of class members using
pragma compiler directive, for instance :
#include <iostream>
using namespace std;
// same as compiler default
#pragma pack (push, 8)
struct PragmaAlign8
{
char a;
double d;
short i;
virtual void v( ) {};
};
#pragma pack( pop )
#pragma pack (push, 2)
struct PragmaAlign2
{
char a;
double d;
short i;
virtual void v( ) {};
};
#pragma pack( pop )
// Compiler default alignment, /Zp8
struct DefaultAlign
{
char a;
double d;
short i;
virtual void v( ) {};
};
int main()
{
cout << sizeof(PragmaAlign8) << endl; // 32
cout << sizeof(PragmaAlign2) << endl; // 16
cout << sizeof(DefaultAlign) << endl; // 32
return 0;
}
Fred Fuzier
|
|
|
|
|