02:13
@CPP, size of classes:

class a
{
private:
char c;
int i;
public:
a()
{
c='a';
i=10;
}
};

sizeof(a) is not 5, but it is 8.
funcitons in class are not counted in sizeof class, because they are shared among all objects of that class.
But, if there are virtual functions then sizeof pointer is added to sizeof class.
 

memory allocation of structure/class goes like this:


-->the data member which occupy the highest size in the given structure/class is examined.
-->memory is allocated as blocks of highest size, 
ex1:
struct a{
char c;
int i;
};
i=>4bytes, memory is allocated as blocks of 4 bytes,
i got 4 bytes, c will also get 4 bytes even it need just one byte. But, the compiler can only give the highest block size of memory.
struct a{
char c;
int i;
double d;
};
d=>8bytes
i=>4B,c=>1(4), it just need 1B, it got 4B.

memory allocated to a structure/class is always multiple of data width of microprocessor.
ex:for 32-bit microprocessor, it is multiple of 4-bytes

this is how it goes......try out your own problems on this.

for more details:
 http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member

packing and align: http://www.c-faq.com/struct/align.html









0 comments: