Sizeof (empty class or empty struct)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.