A child boots went to an ideal international company for an interview the day before yesterday and came back to discuss the following question in the dormitory:
In VC ++, there is an empty class that does not declare any member variables or functions. How many bytes does this empty class occupy?
A, 0 B, 1 C, 4 D, 8
At that time, we considered 32-bit and 64-bit machines, and the pointer address was int type. 32-bit occupies 4 bytes or 64-bit occupies 8 bytes. Therefore, we chose C and D
Then I think about it again. There is no operation pointer, and memory alignment is not required (depending on VC ++ compiler, alignment is automatically optimized). C and D are excluded, and A is selected.
At that time, I also considered the book "Deep Exploration C ++ Object Model" Translated by Hou Jie. How do inheritance and Polymorphism in C ++ be distinguished in the compiler? Therefore, 0 bytes are unreliable.
But at least it will not occupy only one byte, so at that time, B should be thoroughly killed
After the interview, we compiled it on vc6.0, vs2010, G ++ (Linux 2.6.31-14,The result is: 1.
First, I posted the testCode:
# Include <iostream> <br/> using namespace STD; </P> <p> // empty class <br/> class classa <br/>{< br/> }; </P> <p> // inherit the empty class of the empty class <br/> class classb: Public classa <br/>{< br/> }; </P> <p> // empty struct <br/> struct structc <br/>{< br/> }; </P> <p> // main function <br/> int main (INT argc, char ** argv) <br/>{ <br/> cout <"A:" <sizeof (classa) <Endl; <br/> cout <"B: "<sizeof (classb) <Endl; <br/> cout <" C: "<sizeof (structc) <Endl; </P> <p> return 0; <br/>}
Then, compile
The results are as follows:
Vc6.0(XP Professional SP2-32bit)
Vs2010(Win7 ultimate SP1-64bit)
G ++(Ubuntu Linux 2.6.31-14-64bit)
Finally, the analysis result is: 1.
Here, let's take a look at the internal implementation of C ++ polymorphism.
For example, there are three overload functions:
Int add (int A, int B );
Int add (int A, int B, int C );
Float add (float a, float B );
How does the C ++ compiler use the above three functions?
_ Add_int_int
_ Add_int_int_int
_ Add_float_float
The compiler pressure stack records: function name + parameter type + number of parameters (Note: the return value type is insufficient to differentiate polymorphism.)
After learning how the C ++ compiler handles and differentiates polymorphism (with the same overload), now let's go back to the question -- sizeof (empty class or empty struct) = 1
Empty class, no member variables or functions, that is, no content is stored;
However, because empty classes can still be instantiated, that isClassa A; cout <"sizeof (a):" <sizeof (a) <Endl;
When a class can be instantiated, the compiler needs to allocate memory space to it to indicate the address of the class instance.
Here, the compiler allocates a byte (for example, char) by default to mark classes that may be initialized, and at the same time, the space occupied by the empty class is also minimal (that is, 1 byte)