|
-
February 13th, 2008, 12:34 PM
#1
A question regarding the size of a class.
Say we have a class defined as,
class c
{
char c1;
char c2;
int i1;
int i2;
char *ptr;
static int mem;
};
What is the size of the class c on a 32 bit processor? Here is what I think.
char c1: 1 byte
char c2: 1 byte
int i1: 4 bytes
int i2: 4 bytes
char* ptr: 4 bytes
static int mem: 4 bytes
Totally, 18 bytes. Am I right? Thanks for your inputs.
-
February 13th, 2008, 12:40 PM
#2
Re: A question regarding the size of a class.
I do not think the static member will be counted. There may also be padding after the two char members (adding another 2 bytes), making it 4 + 4 + 4 + 4 = 16 bytes.
-
February 13th, 2008, 01:09 PM
#3
Re: A question regarding the size of a class.
Thanks for your response! Why will the static member NOT be counted? As for padding, why isn't there padding for each of c1 and c2? Thanks for your inputs.
 Originally Posted by laserlight
I do not think the static member will be counted. There may also be padding after the two char members (adding another 2 bytes), making it 4 + 4 + 4 + 4 = 16 bytes.
-
February 13th, 2008, 01:16 PM
#4
Re: A question regarding the size of a class.
Why will the static member NOT be counted?
According to the C++ Standard, "a static data member is not part of the subobjects of a class".
As for padding, why isn't there padding for each of c1 and c2?
Since they are adjacent, together they can fit in the space of an int.
-
February 13th, 2008, 01:20 PM
#5
Re: A question regarding the size of a class.
 Originally Posted by laserlight
Since they are adjacent, together they can fit in the space of an int.
Padding is very much implementation dependant. The compiler could (but probably would not) pad between the char fields. The compiler could (and quite probably would) pad after the characters but before the int in order to have a 32 bit integer properly aligned.
As far as the static, think of it this way. You create 1000 instances of the object, there is still only one copy of the static data (that is the very definition). Therefore it is not part of the size of an instance.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
February 13th, 2008, 03:15 PM
#6
Re: A question regarding the size of a class.
Thanks for you guys' help! Now I understand the size of the class could be 16 or 20 bytes depending on the compiler.
-
February 13th, 2008, 08:14 PM
#7
Re: A question regarding the size of a class.
And don't be fooled that the size of the class below is 0 
-
February 13th, 2008, 09:16 PM
#8
Re: A question regarding the size of a class.
... And that you can change the way that the compiler packs the class's structure. For example, in Microsoft's compiler:
Code:
struct st1
{
char c;
int i;
};
#pragma pack ( push )
#pragma pack ( 1 )
struct st2 /* identical to st1 */
{
char c;
int i;
};
#pragma pack( pop )
void main()
{
cout << "Size of st1 :" << sizeof(st1) << endl;
cout << "Size of st2 :" << sizeof(st2) << endl;
}
Output (untested):
Size of st1 : 8
Size of st2 : 5
Mike
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|