C + + Empty class and the size of the class (C + + object-oriented model is mentioned)

Source: Internet
Author: User

Beginners in the study of object-oriented programming language, more or less questions, we write the code and the final compiled code is very different, we do not know what the compiler does in the background to do the work. These are the reasons why we only stay in the language layer, the so-called language layer is to teach us some basic grammatical rules, but will not tell us why do this? Today and we talk about a little bit of understanding is that I am learning the programming process of a little experience, is the compiler this aspect of a specific function.
First of all: we want to know what is the instantiation of a class, the so-called instantiation of the class is to allocate a piece of address in memory.

Let's take a look at an example:

#include <iostream.h>

Class A {};
Class b{};
Class C:public a{
virtual void fun () = 0;
};
Class D:public B,public c{};
int main ()
{
cout<< "sizeof (a)" <<sizeof (a) <<endl;
cout<< "sizeof (b)" <<sizeof (b) <<endl;
cout<< "sizeof (c)" <<sizeof (c) <<endl;
cout<< "sizeof (d)" <<sizeof (d) <<endl;
return 0;}

The output of the program execution is:

sizeof (a) =1

sizeof (b) =1

sizeof (c) =4

sizeof (d) =8

Why is this happening? Beginners must be very upset, right? Class A, B is obviously empty class, its size should be 0, why the compiler output is 1? This is the reason we have just described the instantiation (empty class can also be instantiated), each instance has a unique address in memory, in order to achieve this, the compiler will often give an empty class implicitly add a byte, so that the empty class in memory after instantiation has a unique address. So a, B has a size of 1.

and Class C is derived from the Class A, it has a pure virtual function, because of the reason for the virtual function, there is a pointer to the virtual function (VPTR), the 32-bit system is allocated to the size of the pointer is 4 bytes, so finally the size of Class C is 4.

The size of Class D is more confusing for beginners, Class D is derived from class B,c recently, and its size should be the sum of 5, why is it 8? This is because in order to improve the access efficiency of the instance in memory. The size of the class is often adjusted to an integer multiple of the system. and take the nearest rule, which is the most recent multiple, that is the size of the class, so the size of Class D is 8 bytes.

Of course, the results may vary from one compiler to another, but this experiment tells us that beginners, whether or not classes are empty, can be instantiated (empty classes can also be instantiated), and each instance has a unique address.

I use the compiler for VC + + 6.0.

Let's look at one more example.

#include <iostream.h>
Class a{
Pivate:
int data;
};

Class b{
Private
int data;
static int data1;
};
int B::d ata1=0;
void Mian () {
cout<< "sizeof (a) =" <<sizeof (a) <<endl;
cout<< "sizeof (b) =" <<sizeof (b) <<endl;
}

The result of the execution is:

sizeof (a) = 4;

sizeof (b) = 4;

Why does Class B have a data member that is the same size as Class A? Because: a static data member of Class B is placed in a global data member of the program by the compiler, which is one of the members of the class. However, it does not affect the size of the class, no matter how many instances the class actually produces, or how many new classes are derived, static member data is always present in the class with only one entity, and non-static data members of the class exist only when they are instantiated. However, once a static data member of a class is declared, it already exists, regardless of whether the class is instantiated or not. So to speak, a static data member of a class is a special global variable.

So a, B is the same size.

Let's look at the size of a class with constructors, and destructors, and how big is it?

#include <iostream.h>
Class a{
Public:
A (int a) {
A=x;}
void f (int x) {
Cout<<x<<endl;}
~a () {}

Private
int x;
int g;
};
Class b{
Public
Private
int data; int data2;
static int xs;
};
int b::xs=0;
void Main () {
A s (10);
S.F (10);
cout<< "Sozeof (A)" <<sizeof (a) <<endl;
cout<< "sizeof (b)" <<sizeof (b) <<endl;
The program execution output is:

10,

sizeof (a) 8

sizeof (b) 8

They all have the same result, and you can see that the size of the class is independent of the constructors, destructors, and other member functions in it, only the member data in it.

From the above few examples it is not difficult to find the size of the class:

1. The sum of the type size of the non-static member data for the class.

2. The size of the additional member variables added by the compiler to support certain attributes of the language (for example, pointers to virtual functions).

3. To optimize access efficiency, the edges are adjusted (aligned).

4 is independent of constructors, destructors, and other member functions in a class.

C + + Empty class and the size of the class (C + + object-oriented model is mentioned)

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.