How many bytes does a class object occupy in C++

How many bytes does an empty class have in memory? How big is it after adding a member function? What part of memory is this member function stored in?

How much memory space does a Class object need to occupy. The most authoritative conclusion is: after introducing the theoretical knowledge, take a look and find another example (Note: all the results are the conclusions drawn in the VC6.0 development environment) 1. Size of the empty class

*非静态成员变量总合。
*加上编译器为了CPU计算,作出的数据对齐处理。
*加上为了支持虚函数,产生的额外负担。


class Car
{
};

void main()
{
       int size = 0;
       Car objCar;
       size = sizeof(objCar);
       printf("%s %d /r", "Class Car Size:", size);
}

Output: Class Car Size: 1
Why is this? I think for this question, not only the novice developers who have just entered the industry, but also the developers who have more than a few years of C++ development experience may not be able to explain this clearly.
The compiler needs to make an Object of Class Car after executing Car objCar; after this line of code. And the address of this Object is still unique, so the compiler will create an implicit one-byte space for the empty class.

2. Only the Size of 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 result: Class Car Size: 8
This result is clear to many developers. On 32-bit systems, integer variables occupy 4 bytes. Here Class Car contains two member variables of integral type, so the 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 result: Class Car Size: 8
We added a static member variable to Class Car this time, but the Class Size is still 8 bytes. This is exactly in line with the first article 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 result: Class Car Size: 12
A character variable is inserted into the class, and the result Class Size becomes 12. This is that the compiler adds 3 additional character variables for data alignment processing, in order to improve the computing speed of the CPU. We can't see the extra stuff added by the compiler. This also conforms to the second article in the conclusion: plus the data alignment processing made by the compiler for CPU calculation.
Since, we define class member data like this, the compiler will add extra null. Then, why don't we consider the problem of data alignment when defining classes, we can define 3 more character type variables as reserved variables, which can not only meet the requirements of data alignment, but also add some extensible variables to our own programs. space.

3. Only the Size of member functions

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: Class Car Size: 1
Oh, what's going on here? Do another experiment to see.

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: Class Car Size: 8
This time it should be clear. Functions do not take up class space. The Size in the first example is 1 byte, it is the compiler that creates an implicit one-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 result: Class Car Size: 4
This time, let the destructor be a virtual function, and see that the Class Size is 4. This is exactly the Size of the pointer vptr to the Virtual Table. This is exactly in line with the third point in the conclusion: plus the additional burden of supporting virtual functions.

quote

original

http://blog.ac521.org/?p=717

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325813141&siteId=291194637