The number of inner bytes occupied by a class Object in C ++ (7 examples). class bytes
How many bytes does an empty class have in memory? What if I add a member function? What part of the memory does this member function store?
The size of memory occupied by a Class Object. The most authoritative conclusion is:
* The sum of non-static member variables.
* Added the Data Alignment processing made by the compiler for CPU computing.
* Added the extra burden to support virtual functions.
After introducing the theoretical knowledge, let's look at another example (Note: All the results are drawn in the VC6.0 Development Environment)
I. Size of the null class
1 class Car 2 { 3 }; 4 5 void main() 6 { 7 int size = 0; 8 Car objCar; 9 size = sizeof(objCar);10 printf("%s %d /r", "Class Car Size:", size);11 }
Output result: Class Car Size: 1
Why? I think this question is not only a beginner in development just after entry, but even developers who have more than a few years of C ++ development experience may not be clear about it.
The compiler must execute the Car objCar code to create a Class Car Object. The Object address is unique, so the compiler creates an implicit byte space for the empty class.
2. Only the Size of the member variable
1 class Car 2 { 3 private: 4 int nLength; 5 int nWidth; 6 }; 7 8 void main() 9 {10 int size = 0;11 Car objCar;12 size = sizeof(objCar);13 printf("%s %d /r", "Class Car Size:", size);14 }
Output result: Class Car Size: 8
This result is clear to many developers. In a 32-bit system, the integer variable occupies 4 bytes. Here, Class Car contains two member variables of the integer type, so the Class Size is 8.
3. static member variablesSize
1 class Car 2 { 3 private: 4 int nLength; 5 int nWidth; 6 static int sHigh; 7 }; 8 9 void main()10 {11 int size = 0;12 Car objCar;13 size = sizeof(objCar);14 printf("%s %d /r", "Class Car Size:", size);15 }
Output result: Class Car Size: 8
We added a static member variable in Class Car this time, but the Class Size is still 8 bytes. This is exactly the same as the first one in the conclusion: The sum of non-static member variables.
4. There are character variables (char)Size
1 class Car 2 { 3 private: 4 char chLogo 5 int nLength; 6 int nWidth; 7 static int sHigh; 8 }; 9 10 void main()11 {12 int size = 0;13 Car objCar;14 size = sizeof(objCar);15 printf("%s %d /r", "Class Car Size:", size);16 }
Output result: Class Car Size: 12
An extra variable of the Class is inserted, and the Size of the Class is changed to 12. The compiler adds three character variables for Data Alignment to increase the CPU computing speed. We cannot see anything else added by the compiler. This also conforms to the second article in the conclusion: the Data Alignment processing made by the compiler for CPU computing.
In this case, the class member data compiler will be added with null values. So, why don't we consider Data Alignment when defining classes? We can define three character type variables as reserved variables to meet the requirements of data alignment, I also added some scalable space for my program.
5. Only the Size of the member function
1 class Car 2 { 3 public: 4 Car(){}; 5 ~Car(){}; 6 public: 7 void Fun(){}; 8 }; 9 10 void main()11 {12 int size = 0;13 Car objCar;14 size = sizeof(objCar);15 printf("%s %d /r", "Class Car Size:", size);16 }
Output result: Class Car Size: 1
Oh, what's the problem? Let's try another experiment.
6. Size of member functions and member variables
1 class Car 2 { 3 public: 4 Car(){}; 5 ~Car(){}; 6 public: 7 void Fun(){}; 8 private: 9 int nLength;10 int nWidth;11 };12 13 void main()14 {15 int size = 0;16 Car objCar;17 size = sizeof(objCar);18 printf("%s %d /r", "Class Car Size:", size);19 }
Output result: Class Car Size: 8
It should be clear this time. Functions do not occupy class space. In the first example, the Size is 1 byte, And the compiler creates an implicit byte space for the class.
7. Size of virtual functions
1 class Car 2 { 3 public: 4 Car(){}; 5 virtual ~Car(){}; 6 public: 7 void Fun(){}; 8 }; 9 10 void main()11 {12 int size = 0;13 Car objCar;14 size = sizeof(objCar);15 printf("%s %d /r", "Class Car Size:", size);16 }
Output result: Class Car Size: 4
This time, let the Destructor be a virtual function, and we can see that the Class Size is 4. This is the Size of the vptr pointer pointing to the Virtual Table. This is exactly the same as the third article in the conclusion: In addition, the extra burden generated to support virtual functions.
Http://www.cnblogs.com/findumars/p/7270628.html