C + + Empty class instance size is not 0 reason

Source: Internet
Author: User
Tags in python

Examples of file reading and writing in Python
When beginners are learning object-oriented programming languages, they are more or less doubtful that the code we write is quite different from the code that was compiled in the end, and we don't know what the compiler did in the background. These are the reasons why we only stay in the language layer, the so-called language layer is to teach us some basic grammar rules, but will not tell us why to do so? Today and you talk about a little sentiment is that I learn programming in the process of a little experience, is a compiler this aspect of a specific function.

First of all: we need to know what is the instantiation of the class, the so-called class instantiation is to allocate a piece of address in memory.

Let's take a look at an example first:

#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 kind of result happening? Beginners must be very upset, right? Class A,b is obviously an empty class, its size should be 0, why the compiler output of the result is 1? That's why we just said the instantiation (the empty class can also be instantiated), each instance has a unique address in memory, and in order to do so, the compiler tends to implicitly add a byte to an empty class, so that the empty class gets a unique address in memory after it is instantiated. So the size of the a,b is 1.

and Class C is derived from 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 assigned to the size of the pointer is 4 bytes, so the size of the C class was finally 4.

The size of Class D is more puzzling to beginners, Class D is derived from the class B,c recently, its size should be both and 5, why is 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 the integer multiple of the system. and take the nearest rule, which is the nearest multiple, which is the size of the class, so the size of Class D is 8 bytes.

Of course, the results from different compilers may be different, but this experiment tells us that beginners, regardless of whether the class is empty class, can be instantiated (empty classes can also be instantiated), each instance has a unique address.

I use the compiler for VC + + 6.0.

Let's look at one more example below.

#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 results of the execution are:

sizeof (a) = 4;

sizeof (b) = 4;

Why does Class B have one more data member, but the same size as Class A? Because: the static data member of Class B is placed by the compiler in a global data member of the program, which is a data 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 only exist when they are instantiated. However, once a class's static data member is declared, it already exists, regardless of whether the class is instantiated. It can be said that the static data member of a class is a special global variable.

So the a,b are the same size.

Let's look at the size of a class that has 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 result of the program executing output is:

10,

sizeof (a) 8

sizeof (b) 8

Their results are the same, and you can see that the size of the class is independent of its constructor, destructor, and other member functions, and is related only to the member data in it.

The size of the class is not difficult to find from the few examples above:

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

2. The size of the member variable added by the compiler to support certain attributes of the language (e.g., pointers to virtual functions).

3. To optimize access efficiency, make the edge adjustment.

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

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.