How many bytes are consumed by a class object in C + + (7 examples, very clear)

Source: Internet
Author: User

How many bytes of an empty class are in memory? How big is it if I join a member function? What part of this member function is stored in memory?

How much memory space a class object needs to occupy. The most authoritative conclusions are:
* Total non-static member variables.
* Plus the compiler for CPU calculation, make the data alignment processing.
* Plus an additional burden to support virtual functions.

After introducing the theoretical knowledge, look again for an example (note: All results are in the VC6.0 development environment to draw conclusions)


the size of an empty class

1 classCar2 {3 };4  5 voidMain ()6 {7     intSize =0;8 Car Objcar;9Size =sizeof(Objcar);Tenprintf"%s%d/r","Class Car Size:", size); One}
Output result: Class Car size:1

Why is this? I think for this problem, not only the new development of new entrants, even if there are more than a few years of experience in C + + developers may not be able to say this clearly.
The compiler executes the car objcar; this line of code needs to be followed by a class car object. And the address of this object is unique, so the compiler creates an implicit byte space for the empty class.

size of member variable only

1 classCar2 {3 Private:4     intnlength;5     intnwidth;6 };7  8 voidMain ()9 {Ten     intSize =0; One Car Objcar; ASize =sizeof(Objcar); -printf"%s%d/r","Class Car Size:", size); -}
Output result: Class Car size:8

This result is clear to many developers. In a 32-bit system, an integer variable accounts for 4 bytes. Here class car contains two member variables of integral type, so class size is 8.

Three, a size with a static member variable

1 classCar2 {3 Private:4     intnlength;5     intnwidth;6     Static intShigh;7 };8  9 voidMain ()Ten { One     intSize =0; A Car Objcar; -Size =sizeof(Objcar); -printf"%s%d/r","Class Car Size:", size); the}
Output result: Class Car size:8

We have added a static member variable to class car this time, but the class size is still 8 bytes. This coincides with the first article in the conclusion: the sum of non-static member variables.

Four. A size with a character type variable (char)

1 classCar2 {3 Private:4     CharChlogo5     intnlength;6     intnwidth;7     Static intShigh;8 };9  Ten voidMain () One { A     intSize =0; - Car Objcar; -Size =sizeof(Objcar); theprintf"%s%d/r","Class Car Size:", size); -}
Output result: Class Car size:12

A character variable is inserted into the class, and the result class size becomes 12. This is the compiler added 3 additional character variables, do the data alignment, in order to improve the CPU's computational speed. The compiler added something extra that we couldn't see. This also conforms to the second rule in the conclusion: Add the compiler for CPU calculation, make the data alignment.
Now that we define the class member data compiler this adds extra space. So, why don't we consider the problem of data alignment when we define the class, we can define 3 character type variables as reserved variables, satisfy the requirement of data alignment, and add some extensible space to our program.

the size of the member function only

1 classCar2 {3  Public:4 Car () {};5~Car () {};6  Public:7     voidFun () {};8 };9  Ten voidMain () One { A     intSize =0; - Car Objcar; -Size =sizeof(Objcar); theprintf"%s%d/r","Class Car Size:", size); -}
Output result: Class Car size:1

Oh, what's going on here? Let's do another experiment.

Six. Size with member functions and member variables

1 classCar2 {3  Public:4 Car () {};5~Car () {};6  Public:7     voidFun () {};8 Private:9     intnlength;Ten     intnwidth; One }; A   - voidMain () - { the     intSize =0; - Car Objcar; -Size =sizeof(Objcar); -printf"%s%d/r","Class Car Size:", size); +}
Output result: Class Car size:8

It should be clear this time. The function does not occupy class space. The first example has a size of 1 bytes, which is exactly what the compiler creates for the class an implicit byte of space

size with virtual function (virtual)

1 classCar2 {3  Public:4 Car () {};5     Virtual~Car () {};6  Public:7     voidFun () {};8 };9  Ten voidMain () One { A     intSize =0; - Car Objcar; -Size =sizeof(Objcar); theprintf"%s%d/r","Class Car Size:", size); -}
Output result: Class Car size:4

This time, let the destructor be a virtual function, see the class size is 4. This is the size of the pointer vptr to virtual table. This coincides with the third article in the conclusion: add an additional burden to support virtual functions.

Http://www.cnblogs.com/findumars/p/7270628.html

How many bytes are consumed by a class object in C + + (7 examples, very clear)

Related Article

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.