Size of instantiated objects in C + +

Source: Internet
Author: User

Today I wrote a class that encountered some problems with sizeof () testing the size of the class object.

In general, the size of the object is determined by the size of the data member, not the size of the member function.

On the internet to find a very detailed example, I encountered the problem is the case of three cases, involving the optimization of the compiler.

The reason to write this "C + + class instantiation of the size of the sizeof ()", because in the written test encountered the following such a problem, then feel is this a hole, but, I still do not hesitate to jump down, because there is a blind spot of knowledge. Now, to summarize, you do not know the size of the instantiation object of the C + + class of sizeof ().

Class D
{
Public
D ()
{

}

Virtual ~d ()
{

}

Private
int A;
Char *p;
};

Example one:

Class A
{
};

A;
cout << sizeof (a) << Endl;

Run Result: 1

Explanation: Empty class, no member variable or function, that is, there is no storage content, but by a a, empty class can still be instantiated. A class can be instantiated, and the compiler will need to allocate memory space to indicate the address of the class instance. Here the compiler allocates a byte by default to mark a class instance that might initialize, while leaving the empty class with the least amount of space (that is, 1 bytes).

Example two:

Class B
{
Private
int A;
};
b b;
cout << sizeof (b) << Endl;

Run Result: 4

Explanation: When there are other members in the class occupying space, that one byte is not counted, as in the case: The result is 4, not 1+4=5.

Example three:

Class BB
{
Private
int A;
Char b;
};
BB BB;
cout << sizeof (BB) << Endl;

Run Result: 8

Explanation: What? How could it be 8? Shouldn't it be 4 + 1 = 5? This examines the alignment, which involves compiler optimizations. For most CPUs, the integer multiples of CPU length are faster, so for these members to add up to this integer multiple, it is possible that the compiler will insert superfluous content to fill this integer multiple, and sometimes adjacent members may also be inserted into the blank for this purpose, which is called "fill-in" (padding). Therefore, the C + + standard tightly stipulates that members are arranged in the order defined by the class, but are not required to be tightly aligned in memory. As a result, a char on the previous byte is fully stored and becomes 4 bytes.

Example four:

Class C
{
Private
int A;
Char *p;
};
c C;
cout << sizeof (c) << Endl;

Run Result: 8

Explanation: In general, if it is a pointer, it accounts for 4 bytes of storage regardless of what data type the pointer is pointing to.

Example five:

Class D
{
Public
D ()
{

}

Virtual ~d ()
{

}

Private
int A;
Char *p;
};
D D;
cout << sizeof (d) << Endl;

Run Result: 12

Explanation: Investigate virtual functions. When a class contains virtual functions (whether its own virtual function or inherited), then there is a member variable information in the class: a virtual function pointer (4 bytes), which points to a virtual function table, the first item of the virtual function table is the TypeInfo information of the class, and then the entry is the address of all the virtual functions of the class.

A further explanation: when there are virtual functions in a class, the compiler inserts a table for the class that we don't see as loving you. This table is the virtual function table, and the data we can't see is the pointer to the virtual function table-the virtual table pointer. The virtual function table is to hold the address of the virtual function in the class. We can understand the virtual function table as an array, and each element in the array holds the address of the virtual function in the class. When the virtual function is called, the program does not jump directly to the code of the function as the normal function, but first takes out the virtual table pointer to get the address of the virtual function table, according to this to the virtual function list, from the table to remove the function pointer, and finally call the function.

Example SIX:

Class E
{
Public
E ()
{

}

Virtual ~e ()
{

}

Private
int A;
Char *p;
static int b;
};
e e;
cout << sizeof (e) << Endl;

Run Result: 12

Explanation: Examine memory allocations for static member variables. Because a static member variable allocates space in a static store, it is not part of the instance, so static member variables in the class do not occupy space.

Example Seven:

Class F:public E
{
Public
F ()
{

}

~f ()
{

}

Private
int C;
};
e e;
cout << sizeof (e) << Endl;

Run Result: 16

Explanation: Storage space for derived class objects = space for base class storage + non-static data members unique to derived classes

Example eight:

Class G:public Virtual E
{
Public
G ()
{

}

~g ()
{

}

Private
int C;
};
g G;
cout << sizeof (g) << Endl;

Run Result: 20

Explanation: If it is a virtual inheritance, the storage space size of the class object = storage space of the base class + the storage space of non-static data members that are unique to the derived class + the virtual function storage space for each class (this is additional, that is, by this formula, sizeof (g) = (e base class storage space) + 4 ( G-specific storage space for non-static data members) + 4 (storage space for virtual functions of class E, if there are multiple virtual functions in class E, only one time)).

Example nine:

Class H:public Virtual E
{
Public
H ()
{

}

~h ()
{

}

virtual void GetValue ()
{

}

Private
int C;
};
H H;
cout << sizeof (h) << Endl;

Run Result: 24

Explanation: compare example eight, as explained above: the storage space size of the class object = storage space of the base class + storage space for non-static data members unique to derived classes + virtual function storage space for each class (sizeof (h) = (e base class storage) + 4 ( G-specific storage space for non-static data members) + 4 (the storage space for virtual functions of class E, if there are multiple virtual functions in class E, only one time) + 4 (storage space for virtual functions of class H, if there are multiple virtual functions in the H class, only one time)).

As above, that is my summary of this type, this problem can only appear once!!!

"C + + Design new thinking " Download See http://www.linuxidc.com/Linux/2014-07/104850.htm

C + + Primer Plus 6th edition Chinese version is clearly bookmarked pdf+ source code http://www.linuxidc.com/Linux/2014-05/101227.htm

Read C + + Primer's constructor trap http://www.linuxidc.com/Linux/2011-08/40176.htm

Read the C + + Primer smart pointer http://www.linuxidc.com/Linux/2011-08/40177.htm

Read the C + + Primer handle class http://www.linuxidc.com/Linux/2011-08/40175.htm

Comb the C language and distribute it in the following 10 chapters:

      1. Linux-c Growth Path (i): Linux under C Programming Overview http://www.linuxidc.com/Linux/2014-05/101242.htm
      2. Linux-c Road of Growth (ii): Basic data Type http://www.linuxidc.com/Linux/2014-05/101242p2.htm
      3. Linux-c Growth Path (iii): Basic IO function Operation http://www.linuxidc.com/Linux/2014-05/101242p3.htm
      4. Linux-c Road of Growth (iv): operator http://www.linuxidc.com/Linux/2014-05/101242p4.htm
      5. Linux-c Road of Growth (v): Control flow http://www.linuxidc.com/Linux/2014-05/101242p5.htm
      6. Linux-c growth Path (vi): function Essentials http://www.linuxidc.com/Linux/2014-05/101242p6.htm
      7. LINUX-C growth Path (vii): array and pointer http://www.linuxidc.com/Linux/2014-05/101242p7.htm
      8. Linux-c growth Path (eight): storage class, dynamic memory http://www.linuxidc.com/Linux/2014-05/101242p8.htm
      9. Linux-c Road of Growth (ix): Composite data type http://www.linuxidc.com/Linux/2014-05/101242p9.htm
      10. Linux-c Road of Growth (10): Other advanced Topics Permanent Update link address : http://www.linuxidc.com/Linux/2015-02/112882.htm

Size of instantiated objects in C + + (GO)

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.