Memory size (sizeof) Analysis of classes in C ++, size of Space sizeof

Source: Internet
Author: User

Memory size (sizeof) Analysis of classes in C ++, size of Space sizeof

First, determine the space occupied by each data type. For example, whether int occupies 2 bytes or 4 bytes space:

In TC, int Is 2 bytes (mainly because TC is 16 bits, so int type should also be 16 bits)
In VC ++, int Is 4 bytes, because most of the software in modern operating systems is 32 bits.
64-bit VC ++, originally it should be 8 bytes, but it may be possible to port the 32-bit source code to 64-bit to avoid errors, therefore, the length is 4 bytes.
I have never used other famous compilers, such as gcc. You have to check the length of the specified int.

You can also use sizeof (int. The calculation on my computer is as follows:

In the C language, the size of struct storage space is a deep topic, involving the basic principles of computers and operating systems. I think alignment is a problem that many beginners cannot understand in C language. Especially in cross-platform environments, alignment is more complex and changeable, each system has its own unique alignment mode. In Windows, the maximum body weight and the number of bytes of the built-in storage unit are often used as alignment benchmarks. in Linux, all pairs are aligned in 4 bytes.

What are the special problems about the memory size of classes in C ++?

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

I think alignment is basically set in the operating system. Since there are differences between Linux and Windows, there will certainly be some differences in the C ++ class. With regard to alignment, I think that you only need to remember the alignment rules of the systems used in normal times. In Windows, alignment is usually based on the number of bytes of the largest built-in storage unit of the structure weight, in Linux, all pairs are aligned in 4 bytes.

Secondly, I think we should discuss which members occupy the storage space in the base class, and those Members do not occupy the memory space?

The data objects that occupy the storage range in C ++ are mainly non-static data objects, including various built-in data types and class objects. The function declaration and function definition in the class are not counted as memory space. However, note that all virtual functions (virtual functions) share a memory area, which is generally 4 bytes. Why only include non-static data objects? Because static data does not belong to any object of the class, it is the attribute of the class, rather than the attribute of a specific object. In the whole memory area, there is only one memory area to store the corresponding static data, that is, all class objects share this data, so it cannot be regarded as a specific object or type of memory space.

Therefore, the storage space of the base class object can be considered:

Non-static data member size + 4 bytes (virtual function storage space)

Of course, this size does not overlay the size of all data members, but has an alignment problem. For details, refer to relevant alignment documents.

Finally, I think I must be concerned about the bucket of the derived class?

In C ++, an inherited class is a useful class. Inheritance extends various types based on the base class. At this time, the derived class contains the information of the base class. Generally, 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 of the derived class. Therefore, the storage space of virtual functions cannot be added to the inheritance class (because all virtual functions share a memory area ), you only need to consider the memory size of non-static data members added to the derived class center.

Therefore, the size of the bucket of the derived class object can be considered as follows:

Base-class bucket + the bucket of a non-static data member exclusive to the derived class

Another type is special. In the case of virtual inheritance, the storage space size changes.

The bucket of the base class + the bucket of the non-static data member exclusive to the derived class + the virtual function bucket of each class.

Here are some examples to illustrate the problem:

# Include <iostream> using namespace std; class test {public: test (); private: int a; char c ;}; int main () {cout <sizeof (test) <endl; // system ("pause"); // press Ctrl + F5 return 0 ;}

The above code outputs 8 in linux and windows, instead of 5. This is a topic discussed in C language, but it also needs to be considered in alignment in C ++. The differences between operating systems are described in a uniform example later.

Virtual function problems

To discuss the 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. It only adds a virtual function and then compiles and debugs it. At this time, the output result is 12, that is, after a virtual function is added, the data member of the class is increased by 4 bytes. Does each virtual function occupy 4 bytes? Actually, no. Add a new virtual function virtual void get_a_c () to test. The output result is still 12, which indicates that all virtual functions share four bytes.

Static Data

We know that static data is not an object attribute, but a class attribute. It cannot be regarded as an object or a type of bucket. It can only be declared in the class definition, and initialization can only be performed outside the class, of course there are exceptions. This will not be analyzed. For more information, see the following example.

Bucket of the derived class

The derived class inherits many Members from the base class and adds many Members. Because the virtual function is inherited, the virtual function is not explicitly defined in the derived class, A virtual function inherited from the base class also exists in the derived class. Therefore, the virtual function does not need to calculate the memory space, but only needs to increase the non-static member data size of the base class. As shown in the following definition, this function outputs 20, which is the size of the added non-static data double d storage space. The above analysis is proved.

#include <iostream>using namespace std;class test{public:        test();        virtual ~test();        virtual void get_a_c();private:        int a;        char c;};class derived_test:public test{public:    virtual ~derived_test();private:    double d ; };int main(){    cout << sizeof(derived_test) << endl;    return 0;}

Test the size of the class inherited by virtual machines:

#include <iostream>using namespace std;class A{    char i[3];public:    virtual void a(){};};class B : public virtual A{    char j[3];public:    virtual void b(){}};class C: public virtual B{    char k[3];public:    virtual void c(){}};int main(){    cout << "sizeof(A): " << sizeof(A) << endl;    cout << "sizeof(B): " << sizeof(B) << endl;    cout << "sizeof(C): " << sizeof(C) << endl;    return 0;}

Output result:

In the case of virtual inheritance, the storage space size changes.

The bucket of the base class + the bucket of the non-static data member exclusive to the derived class + the virtual function bucket of each class.

In addition, if Class A is changed to the following format:

class A{    char i[5];public:    virtual void a(){};};

Occupied Space: 12. Because

class A{    char i[9];public:    virtual void a(){};};

Occupied Space: 16. Because

class A{    char i[3];    char t;public:    virtual void a(){};};

Occupied Space: 8. Because

class A{    char i[3];    char t;    char t1;public:    virtual void a(){};};
Occupied Space: 12. Because

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.