c++ - How can I understand the fdump-class-hierarchy output -


i'm playing fdump-class-hierarchy compiler option don't know how can understand output. "size", "align", "base size" , "base align" mean, , how these counted? thanks!

when code is:

class { public:  private:     double m_nothing;     int m_number; }; 

the output is:

class    size=16 align=8    base size=16 base align=8 (0x406c690) 0 

but, if change class little:

class { public:  private:     int m_number;     double m_nothing; }; 

the output be:

class    size=16 align=8    base size=12 base align=8 (0x406c690) 0 

the size , align size , alignment of class when used complete type. is, if create objects complete type type (like defining variables of type, or using type new).

the size number of bytes occupies. size=16 means when used complete type, occupies 16 bytes.

the alignment tells object may placed: align=8 means address of object must integer multiple of 8.

the base size , base align give size , alignment in case class used base class. reason why different c++ standard allows objects use less padding when used base class.

so let's @ example (i'm assuming have int before double in first case). i'm omitting public , private because here don't change (if had both public or private data members, could in principle change something, don't know if compiler takes advantage of that). i'm guessing size , alignment of int , double (actually values assume pretty common choice, , explain values get).

so in first case (i assume) have

class {   int m_number;   double m_nothing; }; 

now int has size , alignment 4, , double has size , alignment 8.

so let's job of compiler , build our class.

first, have m_number, occupies 4 bytes. have put members in order given, m_number goes @ beginning of a:

iiii 

up now, have size 4 (the 4 bytes int), , alignment 4 (because int has alignment 4). have add double (size , alignment 8). since directly after int, @ (relative) address 4, not correctly aligned double, have add 4 padding bytes (which i'll mark *) multiple of 8. our class:

iiii****dddddddd 

now, if class used base class, finished. habe base size=16 , base align=8 (we need alignment of 8 in order double aligned correctly).

for complete object, there's consideration: standard demands in arrays, objects follow each other without gap in between. is, first byte after object must correctly aligned next object. means size of complete object has multiple of alignment.

now object layout we've found fulfils requirement. therefore can use unchanged complete object. therefore size=16 , align=8 complete object.

now consider case order reversed:

class {   double m_nothing;   int m_number; }; 

now have start double:

dddddddd 

next, have add int. turns out, next free place correctly aligned int, therefore can append it:

ddddddddiiii 

now use base object, ready. can see, needed 12 bytes, therefore base size=12. of course double correctly aligned, object again has start @ address multiple of 8. therefore have base align=8.

however sue complete object, find next address @ position 12, not correctly aligned double member. therefore have add padding bytes until reach correctly aligned address again:

ddddddddiiii**** 

as can see, need 16 bytes, size=16. still have align=8 due double.

note alignment requirement can dramatically affect size of class. consider example following 2 types:

struct s1 {   char c1;   double d1;   char c2;   double d2;   char c3; };  struct s2 {   double d1;   double d2;   char c1;   char c2;   char c3; }; 

while both contain same members, s1 sizes , alignments above have total (non-base) size of 40, while total size of s2 24. indeed, objects of type s1 will, complete object, like

c*******ddddddddc*******ddddddddc******* 

while of type s2 like

ddddddddddddddddccc***** 

so bottom line members highest alignment requirement should come first.

also note sizeof returns size of complete objects, is, class hierarchy dump calls size.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -