How much memory does the C ++ object occupy?

Source: Internet
Author: User

A few days ago, I was asked how much memory does it occupy when I set up an empty class in C ++ with no members? I suddenly lost it, and I really didn't think about it. Later, I checked the information and finally understood it.

First, let's take a look at this test program:

#include <iostream>using namespace std; class Test{};main(){Test t;cout<<"size :"<<sizeof(t)<<endl;}

The output result is: Size: 1.

Why do no members of this class need to use 1 byte of memory? The original C ++ requires that each instance have a unique address in the memory, and the empty class will be instantiated, so the compiler will implicitly Add a byte to the empty class. In this way, the empty class has a unique address after instantiation. Once the class has a common member variable, the compiler will not add a byte to the class. In the preceding example, if a public int X is added to the test class, the output result is 4.

Second, let's look at another situation: class with static member variables and functions

#include <iostream>using namespace std; class Test{public:static void hello(){}static int x;};main(){Test t;cout<<"size of Test:"<<sizeof(t)<<endl;}


The output result is: Size: 1.

This is because static members exist independently of any object and are not part of a Class Object. The memory occupied by the object is a byte or is it because the compiler implicitly adds a byte.

Third, common member functions and common member variables

#include <iostream>using namespace std; class Test1{public:int x;};class Test2{public:int x;void hello(){}};main(){Test1 t1;Test2 t2;cout<<"t1 size :"<<sizeof(t1)<<endl;cout<<"t2 size :"<<sizeof(t2)<<endl;}

The output result is: T1 size: 4.

T2 size: 4

This indicates that common member functions do not occupy the class memory space, but common member variables occupy the class memory. All functions in C ++ are stored in the code area, whether it is a global function or a member function.

Fourth, let's take a look at the classes that contain virtual member functions:

#include <iostream>using namespace std; class Test1{public:int x;};class Test2{public:int x;virtual void hello(){}};class Test3{public:int x;virtual void hello(){}virtual void hello2(){}};main(){Test1 t1;Test2 t2;Test3 t3;cout<<"t1 size :"<<sizeof(t1)<<endl;cout<<"t2 size :"<<sizeof(t2)<<endl;cout<<"t3 size :"<<sizeof(t3)<<endl;}

The output result is: T1 size: 4.

T2 size: 8

T3 size: 8

In this example, we can see that, due to the influence of the virtual function, the memory occupied by the Test2 class has increased by four bytes, but T3 does not have four more bytes than t2. Why is it soy sauce? This is because virtual member functions (including pure virtual functions) occupy the memory. Class will have a vptr pointing to the virtual table of the virtual function table. No matter how many virtual functions there are, each class has only one vptr. Therefore, no matter how many virtual functions there are, they only occupy the memory space of sizeof (vptr) = 4 bytes.

Fifth, the derived class: the memory occupied by the derived class must be added with the memory occupied by the base class!

#include <iostream>using namespace std; class Test1{public:int x;};class Test2:public Test1{public:int y;};main(){Test1 t1;Test2 t2;cout<<"t1 size :"<<sizeof(t1)<<endl;cout<<"t2 size :"<<sizeof(t2)<<endl;}

The output result is: T1 size: 4.

T2 size: 8

The final summary is:

1. Common member functions do not occupy memory, but common member variables occupy memory.

2. Static variables and functions do not occupy memory.

3. Virtual member functions (including pure virtual functions) occupy memory because a vptr points to the virtual function table.

4. Add the memory occupied by the base class to the derived class.

5. For empty classes, the compiler automatically adds a 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.