The following discussion is the result of not using #pragma pack (num), where num indicates how many bytes are aligned.
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 stay at the language level, and the so-called language layer teaches us some basic grammatical rules, but it does not tell us why. 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
Above is the result of compiling in vc++6.0, but the result under dev-c++ and Code::Blocks is sizeof (d) = 4
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 more than one data member, but the size is 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 it is.
#include <iostream.h>
class a{public
:
A (int A) {
x=a;
}
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 ();
S.F (ten);
cout<< "Sozeof (A)" <<sizeof (a) <<endl;
cout<< "sizeof (b)" <<sizeof (b) <<endl;
}
The result of the program execution 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.