sizeof (Class)

Source: Internet
Author: User

What is the size of the class? To be exact, a class is just a type definition, and it is no size. Using the sizeof operator for a type name operation, you get the size of the entity with that type. 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

Using sizeof to manipulate the class name, the result is that the object of the class occupies a byte size in memory, because the static member variable is not stored in the object, so the result equals the sum of the non-static data members (excluding member functions) plus the extra bytes added by the compiler. The latter relies on different compiler implementations, which are not guaranteed by the C + + standard.

Several principles for determining the class size:
    • The sum of the type size of the non-static member data for the class
    • The size of a member variable added by the compiler to support some of the language's attributes (for example, pointers to virtual functions)
    • In order to optimize access efficiency, the edge adjustment
    • Independent of constructors, destructors, and other member functions in a class
The following is a discussion of the situation:

Compiler: vs2013

1. Empty class

using namespace std; class a{}; int Main () {    sizeof(A) << Endl;    System ("pause");}

Output: 1

The C + + standard specifies that the size of the class is not 0, the size of the empty class is 1, and that when the class does not contain virtual functions and non-static data members, its object size is also 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 the instantiation of a unique address

2. Simple class

Using sizeof to find this simple class, the result is the same as the sizeof for the struct, and it is necessary to consider the offsets and alignments. Note that the static variable is not part of the class, and if static variables are defined in the class, you can ignore them when you ask sizeof.

#include <iostream>using namespacestd;classa{intA;};classb{CharA;};classc{intA; Charb;};intMain () {cout<<sizeof(A) <<Endl; cout<<sizeof(B) <<Endl; cout<<sizeof(C) <<Endl; System ("Pause");}

Output:

4 sizeof (int)

1 sizeof (char)

8 sizeof (int) + sizeof (char) (consider alignment)

3. Classes with virtual functions

Virtual functions are placed in virtual tables, and virtual functions are defined in a class, and a pointer to a virtual table is required

If a virtual function is declared in a class (whether one or more), then when instantiating an object, the compiler automatically placed a pointer in the object pointing to the virtual function table vtable, on a 32-bit machine, an object adds 4 bytes to store the pointer, which is the key to implementing polymorphism in object-oriented. The virtual function itself, like any other member function, is a space that does not occupy the object.

#include <iostream>using namespacestd;classa{intA; Virtual voidFun () {}Virtual voidfun1 () {}Virtual voidfun3 () {}};intMain () {cout<<sizeof(A) <<Endl; System ("Pause");}

Output

8 sizeof (int) + sizeof (virtual table pointer)

4. Normal inheritance (parent class does not contain virtual functions)

#include <iostream>using namespacestd;classa{intnum; Charstr;};classB: Publica{Charstr2; intnum2;};intMain () {cout<<sizeof(A) <<Endl; cout<<sizeof(B) <<Endl; System ("Pause");}

Output:

8 sizeof (Class A)

sizeof (Class A) + Sizeo (class B)

In general, the spatial calculation of ordinary inheritance should be sizeof (base class) +sizeof (derived class), and then consider the alignment, memory space must be the maximum amount of space occupied by the data members in the class. But this is the general situation, specifically how to look at the compiler, Codeblocks Class B as 12, because put str2 and str together

5. Common inheritance (parent class with virtual function)

#include <iostream>using namespacestd;classa{intnum; Virtual voidFun () {}};classD | Publica{intnum2;};intMain () {cout<<sizeof(A) <<Endl; cout<<sizeof(B) <<Endl; System ("Pause");}

Output:

8 sizeof (Class A)

sizeof (Class A) + Sizeo (class B)

6. Common inheritance (subclasses with virtual functions inherit the parent class with virtual functions)

The point to note is that although both subclasses and parent classes contain virtual functions, they are stored in the same virtual table, so only one pointer is needed

#include <iostream>using namespacestd;classa{intnum; Virtual voidFun () {}};classD | Publica{intnum2; Virtual voidfun1 () {}};intMain () {cout<<sizeof(A) <<Endl; cout<<sizeof(B) <<Endl; System ("Pause");}

Output:

8 sizeof (int) + sizeof (pointer)

sizeof (int) + sizeof (int) + sizeof (pointer) (only one virtual table after inheritance)

7. Subclass Virtual Inheritance Parent class

sizeof (subclass) =sizeof (base class) +sizeof (virtual table pointer) +sizeof (subclass data member) In addition, if both subclasses and base classes have virtual functions, each with their own virtual table

#include <iostream>using namespacestd;classa{intnum;};classD |Virtual  Publica{intnum2;};intMain () {cout<<sizeof(A) <<Endl; cout<<sizeof(B) <<Endl; System ("Pause");}

Output:

4

sizeof (A) + sizeof (B) + sizeof (virtual inheritance pointer)

#include <iostream>using namespacestd;classa{intnum; Virtual voidFun () {}};classB:Virtual  Publica{intnum2; Virtual voidfun1 () {}};intMain () {cout<<sizeof(A) <<Endl; cout<<sizeof(B) <<Endl; System ("Pause");}

Output:

8

sizeof (a) + sizeof (b) + sizeof (virtual inheritance pointer) + sizeof (Class A virtual table pointer) + sizeof (class B virtual table virtual pointer)

8. Multiple Virtual inheritance

The meaning of virtual inheritance is to reduce memory cost and two semantics, and to realize object sharing.

#include <iostream>using namespacestd;classa{intnum;};classD |Virtual  Publica{intnum2;};classE |Virtual  Publica{intnum3;};classD: PublicB Publicc{intnum4;};intMain () {cout<<sizeof(A) <<Endl; cout<<sizeof(B) <<Endl; cout<<sizeof(C) <<Endl; cout<<sizeof(D) <<Endl; System ("Pause");}

Output:

4

12

12

24

D contains a,b,c,d four data members and two pointers to virtual base class A, in which case the VS and CB do not have two pointers together. This situation can also be considered, sizeof (d) =sizeof (B) +sizeof (C), but because of the virtual inheritance, the virtual base class A data member A only need to keep one copy, and we forget two times, so we also need to subtract the data member of a, so the formula should be sizeof (d) = sizeof (non-static data member of D) + sizeof (B) +sizeof (C)-sizeof (non-static data member of a).

Reference: http://blog.csdn.net/szchtx/article/details/10254007

sizeof (Class)

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.