Article From:https://www.cnblogs.com/oneway1990/p/9125604.html

After leaving home for more than half a year, I haven’t written a single line of code for half a year, but I’ve read some books, but almost all of them are jujube. The house is about to be renovated, so we must find a job quickly, otherwise we will have to keep a car and play photography. Ha-ha。

It is customary to dig out some classic books and review them first. In the process of reviewing, we also record some things, saying that it is a Book note, and that it is a good idea. In a word, the purpose of writing is to urge yourself and to review it seriously; the primary purpose is to facilitate some friends and search for learning in the network. hisActually, such basic knowledge is everywhere on the Internet, but I still hope that I can summarize some characteristics.

 

No more gossip. Many C++ books have introduced how much memory space a Class object needs to occupy. The most authoritative conclusion is:

*The combination of non static member variables.

*The compiler also compacts data for CPU computation.

*Plus the extra burden to support virtual functions.

 

After introducing the theoretical knowledge, let’s look at another example to see. (Note: all the results are in the VC6.0 development environment).

1. The Size of the empty class

class Car

{

};

 

void main()

{

       int size = 0;

       Car objCar;

       size = sizeof(objCar);

       printf(“%s %d /r”, “Class Car Size:”, size);

}

 

Output results: Class Car Size:1

 

What is this? I think the problem is not only a newly developed novice, but even the developers who have experienced C++ development experience for more than a few years may not be able to make it clear.

The compiler needs to make a Object of Class Car after executing Car objCar. And the address of the Object is unique, so the compiler creates an implicit byte space for the empty class.

 

 

Two. Only Size of the member variables

class Car

{

private:

       int nLength;

       int nWidth;

};

 

void main()

{

       int size = 0;

       Car objCar;

       size = sizeof(objCar);

       printf(“%s %d /r”, “Class Car Size:”, size);

}

 

Output results: Class Car Size:8

The result is clear to many developers. In the 32 bit system, the integer variable accounts for 4 bytes. Here Class Car contains two integer type member variables, so Class Size is 8.

 

class Car

{

private:

       int nLength;

       int nWidth;

       static int sHigh;

};

 

void main()

{

       int size = 0;

       Car objCar;

       size = sizeof(objCar);

       printf(“%s %d /r”, “Class Car Size:”, size);

}

Output results: Class Car Size:8

We added a static member variable in Class Car this time, but Class Size is still 8 bytes. This is exactly the first in the conclusion: the sum of non static member variables.

 

class Car

{

private:

       char chLogo

       int nLength;

       int nWidth;

       static int sHigh;

};

 

void main()

{

       int size = 0;

       Car objCar;

       size = sizeof(objCar);

       printf(“%s %d /r”, “Class Car Size:”, size);

}

Output results: Class Car Size:12

A character variable is inserted in the class, and the result is that Class Size becomes 12. This is an additional 3 character variables added by the compiler to do data alignment, in order to improve the computing speed of CPU. What the compiler adds is something we can’t see. It also conforms to the conclusionSecond: add the data alignment processing of the compiler for CPU calculation.

Now that we define class members, the data compiler adds extra space. So why don’t we consider the problem of data alignment when we define a class, we can define more than 3 character type variables as reserved variables, which can satisfy the requirements of data alignment and add some extensible to their own programs.Space.

 

Three. Only the Size of the member function

class Car

{

public:

       Car(){};

       ~Car(){};

public:

       void Fun(){};

};

 

void main()

{

       int size = 0;

       Car objCar;

       size = sizeof(objCar);

       printf(“%s %d /r”, “Class Car Size:”, size);

}

Output results: Class Car Size:1

Oh, what’s the matter? Do an experiment and look at it.

class Car

{

public:

       Car(){};

       ~Car(){};

public:

       void Fun(){};

private:

       int nLength;

       int nWidth;

};

 

void main()

{

       int size = 0;

       Car objCar;

       size = sizeof(objCar);

       printf(“%s %d /r”, “Class Car Size:”, size);

}

Output results: Class Car Size:8

It should be clear this time. A function does not occupy a class space. The Size in the first example is 1 bytes. It is the compiler that creates a hidden byte space for the class.

 

class Car

{

public:

       Car(){};

       virtual ~Car(){};

public:

       void Fun(){};

};

 

void main()

{

       int size = 0;

       Car objCar;

       size = sizeof(objCar);

       printf(“%s %d /r”, “Class Car Size:”, size);

}

Output results: Class Car Size:4

This time, let the destructor function as a virtual function and see that the Class Size is 4. This is the Size pointing to the pointer VPTR of Virtual Table. This coincides with the third in the conclusion: plus the additional burden generated to support the virtual function.

 

So far, how much memory space a Class Object occupies has been fully stated. However, this is only applicable to a single class, or a base class. For subclasses, they are different. Interested friends can do some experiments.

Leave a Reply

Your email address will not be published. Required fields are marked *