Memory space Size (sizeof) analysis for classes in C + +

Source: Internet
Author: User

First, make clear how much space each data type occupies. For example, whether int is a 2-byte or 4-byte space:

In TC, int is 2 bytes (mainly because the TC is 16 bits, so the int type is also 16 bits)
VC + +, int is 4 bytes, because most of the software under the modern operating system is 32 bits.
64-bit VC + +, it is supposed to be 8 bytes, but it is possible to maintain 32-bit source code porting to 64 bits as far as possible error-prone, so also maintain the length of 4 bytes.
As for other well-known compilers, like GCC, I haven't used them, you have to check the length of the int that it specifies.

Or it can be calculated using sizeof (int). I calculate the following on the computer:

In C language, there is a more in-depth discussion about the size of the storage space of the structure, which involves the basic principle of computer, operating system and so on. I think alignment is a C language that many beginners are unsure of the problem, especially in the case of cross-platform, the alignment of this problem is more complex and changeable, each system has its own unique alignment, in Windows is often the structure of the largest built-in type of storage units of the number of bytes as a benchmark for alignment, In Linux, all alignments are aligned in 4-byte order.

So what are the special problems with the size of the memory space of the classes in C + +?

First of all, I think alignment is certainly one of the problems, the alignment is mainly to speed up the reading.

About alignment This I think is basically the operating system is well-established, since Linux and Windows are different, then in the C + + class, there must be a certain difference in alignment. About alignment I think just remember the alignment guidelines for the systems that you normally use, that is, the number of bytes in Windows that are often the largest built-in type of storage unit of the structure weight as the justification, whereas in Linux all alignment is 4-byte aligned.

Secondly, I think it is important to discuss which members in the base class occupy the storage space, and those members do not occupy memory space?

The main non-static data objects in C + +, which mainly include various built-in data types, class objects, function declarations in classes, and function definitions, are not memory spaces. However, it is important to note that all virtual functions (virtual functions) share a memory area, typically 4 bytes. Why do you just include non-static data objects? Because the static data does not belong to any object of the class, it is the property of the class, not the property of the specific object, there is only one memory region in the entire memory area to store the corresponding static data, that is, all class objects share this data, So you can't count the memory space of a specific object or type.

Therefore, you can assume that the storage space size of the base class object is:

size of non-static data members + 4 bytes (storage space for virtual functions)

Of course, this size is not all data member size overlay, but there is a alignment problem, the specific should refer to the relevant alignment article.

Finally, I think I'm going to be concerned about the storage space of the derived class?

In C + +, an inherited class is a useful class, and inheritance allows classes to expand on the base class, when the derived class contains information about the base class, in general, when there is a virtual function in the base class, the derived class inherits the virtual function of the base class, so the derived class has inherited the virtual function. Therefore, the storage space of the virtual function can no longer be added in the inheriting class (because all virtual functions share a chunk of memory), but only the size of the memory space of the non-static data members added by the derived class center should be considered.

Therefore, you can think of the size of the derived class object as the storage space:

base class storage space + storage space for non-static data members specific to derived classes

there is a special case, if the virtual inheritance of the case, then the size of the storage space will be changed.

Storage space for the base class + storage space for non-static data members unique to derived classes + virtual function storage for each class.

Let me take a few examples to illustrate the above questions:

#include <iostream>using namespacestd;classtest{ Public: Test ();Private:        intA; Charc;};intMain () {cout<<sizeof(test) <<Endl; //System ("pause");//Press Ctrl+f5    return 0;}

The above code will output 8, not output 5, under Linux and Windows, which is a topic that has been discussed in the C language, but the justification is also considered in C + +. The differences about the operating system are explained in a uniform example later.

virtual function problems

To discuss virtual functions, we add a virtual destructor to the test class and then test the results.

class test{public:        Test ();         Virtual ~test (); Private :         int A;         Char c;};

This code is no different from the previous code, just add a virtual function, and then compile debugging, this time the output of the result of 12, that is, the addition of a virtual function, the class of data members increased by 4 bytes, then whether each virtual function occupies 4 bytes? In fact, it is not, add a new virtual function in test virtual void Get_a_c (), when the result of the output is still 12, which shows that all virtual functions share 4 bytes.

static data

We know that static data is a non-object property, but rather a property of a class, that he cannot be considered a storage space for an object or type, but only declared in the class definition, and that initialization can only be performed outside of the class, with the exception of course. It's not an analysis either. See the big example later.

Storage space for derived classes

Derived classes inherit a lot of members from the base class, and they also add a lot of members, because virtual functions are also inherited, so the virtual function is not explicitly defined in the derived class, there will be virtual functions inherited from the base class in the derived class, so the virtual function does not require additional computational memory space. Instead, you only need to increase the non-static member data size of the base class. The definition, as shown below, outputs 20, just the amount of storage space for the added non-static data double D. proves the above analysis.

#include <iostream>using namespacestd;classtest{ Public: Test (); Virtual~test (); Virtual voidGet_a_c ();Private:        intA; Charc;};classDerived_test: Publictest{ Public:    Virtual~derived_test ();Private:    DoubleD;};intMain () {cout<<sizeof(derived_test) <<Endl; return 0;}

To test the size of a virtual inherited class:

#include <iostream>using namespacestd;classa{Chari[3]; Public:    Virtual voidA () {};};classD | Public Virtuala{Charj[3]; Public:    Virtual voidB () {}};classC: Public Virtualb{Chark[3]; Public:    Virtual voidC () {}};intMain () {cout<<"sizeof (A):"<<sizeof(A) <<Endl; cout<<"sizeof (B):"<<sizeof(B) <<Endl; cout<<"sizeof (C):"<<sizeof(C) <<Endl; return 0;}

The output is:

In the case of virtual inheritance, the size of the storage space will change.

Storage space for the base class + storage space for non-static data members unique to derived classes + virtual function storage for each class.

Also, if Class A is changed to the following form:

class a{    char i[5];  Public :     Virtual void A () {};};

Space occupied: 12. Because

class a{    char i[9];  Public :     Virtual void A () {};};

Space occupied: 16. Because

class a{    char i[3];     Char T;  Public :     Virtual void A () {};};

Space occupied: 8. Because

class a{    char i[3];     Char t;     Char T1;  Public :     Virtual void A () {};};
Space occupied: 12. Because

Memory space Size (sizeof) analysis for classes in C + +

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.