Size of a class

S

Salman

I want to know abt the size of the class using sizeof function. i have
pasted 2 programs. Both gives different sizes of the class just by re-
arranging the order of the private varibles. Tell me whats the reason
behind it. Im pasting both the programs. I using VC++ 6
---------------Program 1 ----------------------------
#include <iostream>
using namespace std;

class A {

char a;
char b;
int* c;

};

int main(void) {

A a;
cout<<sizeof(a);

return 0;
}

---------------Program 2 ----------------------------
#include <iostream>
using namespace std;

class A {

char a;
int* c;
char b;



};

int main(void) {

A a;
cout<<sizeof(a);

return 0;
}
 
R

red floyd

Salman said:
I want to know abt the size of the class using sizeof function. i have
pasted 2 programs. Both gives different sizes of the class just by re-
arranging the order of the private varibles. Tell me whats the reason
behind it. Im pasting both the programs. I using VC++ 6
class A {

char a;
char b;
int* c;

};

versus
class A {

char a;
int* c;
char b;
};


The compiler is free to introduce any padding necessary for alignment
purposes. My guess is that you are seeing 8 for the first and 12 for
the second.
 
M

Marcus Kwok

Salman said:
I want to know abt the size of the class using sizeof function. i have
pasted 2 programs. Both gives different sizes of the class just by re-
arranging the order of the private varibles. Tell me whats the reason
behind it. Im pasting both the programs. I using VC++ 6
---------------Program 1 ----------------------------
class A {

char a;
char b;
int* c;

};

---------------Program 2 ----------------------------
class A {

char a;
int* c;
char b;

};

This is implementation-dependent, but it has to do with alignment. On
some architectures, variables may be required to start at certain
addresses, or the computer may be able to access them much more
efficiently if they start at certain addresses. Therefore, the compiler
is free to add padding to meet these alignment restrictions.

For example, suppose you are on a 32-bit architecture, char is 8 bits,
and int* is 32 bits. Also suppose that the alignment rules for your
system say that int* must start at the beginning of a 32-bit word (i.e.,
a multiple of 4 bytes/octets).

In the first case, suppose char a has a starting address of Start. Then
char b might be placed at (Start + 1). Then, in order to get int* c to
meet the alignment requirement, the compiler will add 2 bytes of padding
between b and c, so that c will start at (Start + 4).

In the second case, char a will be at Start. Then, the compiler will
add 3 bytes of padding so that c starts at (Start + 4). Finally, char b
will be placed at the next available slot, (Start + 8). Then, in order
to account for the situation where e.g. classes need to also start at
the beginning of a word (like when placing them in an array), it may
even add 3 extra padding bytes after b in order to make the entire class
a multiple of 4 bytes.

Some compilers have switches that will let you control whether or not
padding is used. However, as said earlier, this is system dependent.
 
V

VCLover

Salman,

Your question is really interesting and many C++ programmers never
notice this point. This is all due the way memory is allocated
internally for class members. I'm trying to put something for you
below that will help you get what exactly is the story behind this:

1) We know that a "char" takes 1 byte in memory.

2) Memory internally organised in chunks of bytes and depending on
this chunk size we determine what is the word size of the computer.
You might have heard of 16-bit, 32-bit, 64-bit computer.

3) It means when memory being allocated, it is tried to first allocate
the memory from one word as far as possible. That's the reason when
you put 2 chars in a sequence it is allocated 2 consecutive bytes of
the same "word". Note here that if you're using 32-bit computer - word
size is 4 bytes - it means even if you add 2 more char members
consecutively they will occupy only 4 bytes. But as you allocate 5th
char it needs another word (i.e. next 4 bytes).

4) When you declare int *c, it requires 4 bytes while 2 bytes already
allocated to chars a and b, it means not enough space to accomodate
complete int* so it will look for next word i.e. next 4 bytes.

5) This way char a, char b occuply first word (4 bytes). Note that
only 2 bytes are in use. 2 are unused. int *c occupies next word (i.e.
4 bytes) so making a total of 8 bytes.

6) Now when you rearrange char a, int *c, char b. In this instance
char a uses first word, int *c uses second word, and char b uses third
word, i.e. a total of 12 bytes.

Hope it makes sense.

Cheers,
VCLover


Try this class declaration:

class A {
char a;
int* c;
char b;
int* f;
char d;
char e;
};

Size of this class is 20 bytes on a 32-bit computer.
 
S

Salman

Please note that sizeof is *not* a function, it is a compile time
operator.

Thanks alot all of u for providing me such great answers, also thanks
to tragomaskhalos for correcting me.
 
J

Jerry Coffin

I want to know abt the size of the class using sizeof function. i have
pasted 2 programs. Both gives different sizes of the class just by re-
arranging the order of the private varibles. Tell me whats the reason
behind it. Im pasting both the programs. I using VC++ 6

That's not at all surprising -- the first item in a struct/class has to
be at the very beginning of the class. The compiler can insert padding
bytes after any member to align a subsequent member to a boundary that
may be necessary for access of that size of item, or perhaps just to
optimize access to that item.

The required and/or desired alignment for an item typically depends on
the item's size. A struct/class will typically have its smallest size
when arrange the members in descending order by size. Of course, the
sizes of some types can vary from one implementation to another, but you
can usually at least come close (e.g. the size-ordering of integer types
is specified in the standard).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top