Home > Back-end >  C class size (sizeof) in question
C class size (sizeof) in question

Time:09-24

Base class and derived class assumes that goes like this:
 
The class base {
Public:
Int a=0;
Char c='a';
};
The class derived: public base {
Public:
Int aa=1;
Double bb=0.0;
};
cout

Output: 8 and 24

The first 8 I understand, but why is derived class size 24?
If only members of the derived class aa, size of 12, as long as join members of the double, int 8 bytes, is this why?

CodePudding user response:

Estimation is double bb need to be aligned to a 64 - bit address boundary, so the front is filled with 4 bytes,

CodePudding user response:

Maximum member is double, so size is sizeof (double) integer times,

CodePudding user response:

Remove the char c they became 16...

CodePudding user response:

refer to the second floor akirya response:
maximum member is double, so size is sizeof (double) integer times,

What is the concept of automatic alignment? Not according to the compiler figures fixed?

CodePudding user response:

The
reference 3 floor DelphiGuy response:
remove the char c they became 16...

Yes, if really according to the size of the largest members are concerned,

CodePudding user response:

reference 5 floor special_hg reply:
Quote: refer to the third floor DelphiGuy response:

Remove the char c they became 16...

Yes, if really according to the size of the largest members,


Is related to, but it is not aligned to the members of the largest integer times, it should be less than or equal to 32 members aligned to the border of the 32-bit address, greater than 32 bit less than or equal to 64 members of the aligned to a 64 - bit address boundaries... And so on,

CodePudding user response:

refer to 6th floor DelphiGuy response:
Quote: refer to the fifth floor special_hg reply:

Quote: refer to the third floor DelphiGuy response:

Remove the char c they became 16...

Yes, if really according to the size of the largest members,


Is related to, but it is not aligned to the members of the largest integer times, it should be less than or equal to 32 members aligned to the border of the 32-bit address, greater than 32 bit less than or equal to 64 members of the aligned to a 64 - bit address boundaries... And so on,

According to your struct {x double a; Char b; }; How much should be

CodePudding user response:

reference 4 floor special_hg response:
Quote: refer to the second floor akirya response:

Maximum member is double, so size is sizeof (double) integer times,

What is the concept of automatic alignment? Not according to the compiler figures fixed?


T type of starting position is sizeof (T) integer times,

If T is a structure, the structure of the starting position is the largest structure element integer times,

CodePudding user response:

refer to 7th floor akirya response:
Quote: refer to the sixth floor DelphiGuy response:

Quote: refer to the fifth floor special_hg reply:

Quote: refer to the third floor DelphiGuy response:

Remove the char c they became 16...

Yes, if really according to the size of the largest members,


Is related to, but it is not aligned to the members of the largest integer times, it should be less than or equal to 32 members aligned to the border of the 32-bit address, greater than 32 bit less than or equal to 64 members of the aligned to a 64 - bit address boundaries... And so on,

According to your struct {x double a; Char b; }; How much should be


Structure member aligned or not influenced by compiler options, and the size is not the same thing as a class member aligned, you can add a # pragma pack in front of the program (4), # pragma pack (1) look at the output effect, such as

CodePudding user response:

references 9 f DelphiGuy response:
Quote: refer to 7th floor akirya response:

Quote: refer to the sixth floor DelphiGuy response:

Quote: refer to the fifth floor special_hg reply:

Quote: refer to the third floor DelphiGuy response:

Remove the char c they became 16...

Yes, if really according to the size of the largest members,


Is related to, but it is not aligned to the members of the largest integer times, it should be less than or equal to 32 members aligned to the border of the 32-bit address, greater than 32 bit less than or equal to 64 members of the aligned to a 64 - bit address boundaries... And so on,

According to your struct {x double a; Char b; }; How much should be


Structure member aligned or not influenced by compiler options, and the size is not the same thing as a class member aligned, you can add a # pragma pack in front of the program (4), # pragma pack (1) look at the output effect, such as

These I know
Is not discussed by default?

CodePudding user response:

The compiler default byte alignment function

You can force it according to the specified number of bytes aligned
//# pragma pack (1) according to the single byte alignment

The output is May 17

CodePudding user response:

1, general alignment is 1 set, 4-trichlorobenzene byte alignment, general defaults to 4 bytes (maximum 8 bytes), first address must be within the structure of the structure of the integer times of the most wide type address; In addition, the structure of every member of the start address must be integer times the size of their types (need to pay special attention to is under Windows, but the highest under Linux GCC compiler is 4 bytes aligned


2, the structure of the overall size must be divisible by alignment value, the default 4 (the default, and structure of the type size is smaller than the default 4),

3, the structure of the overall size must be within this structure can be divided exactly by the widest type, you have a DOUBLE in the derived class, so the size of the structure of the final must be divisible by 8

Used to test the GCC supreme can only be divided exactly by 4, no test recently, don't know whether there is a change

CodePudding user response:

refer to 12 floor silverpot reply:
1, the general set of alignment is 1, 4-trichlorobenzene byte alignment, general defaults to 4 bytes (maximum 8 bytes), first address must be within the structure of the structure of the integer times of the most wide type address; In addition, the structure of every member of the start address must be integer times the size of their types (need to pay special attention to is under Windows, but the highest under Linux GCC compiler is 4 bytes aligned


2, the structure of the overall size must be divisible by alignment value, the default 4 (the default, and structure of the type size is smaller than the default 4),

3, the structure of the overall size must be within this structure can be divided exactly by the widest type, you have a DOUBLE in the derived class, so the size of the structure of the final must be divisible by 8

Used to test the GCC supreme can only be divided exactly by 4, no test recently, do not know whether have change

Because usually double is 8 bytes, so the default is eight,nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related