C + + class occupies memory size calculation

Source: Internet
Author: User

C + + class occupies memory size calculation

Description: The author's operating system is 32-bit.

Class A {};
sizeof (A) =?
sizeof (A) = 1
Obviously is empty class, why does the compiler say it is 1?
Empty classes can also be instantiated, each instance has a unique address in memory, for this purpose, the compiler will often give an empty class implicitly add a byte, so that the empty class after instantiation in memory to get a unique address. So the size of sizeof (A) is 1.


Class B
{
Public
B () {}
~b () {}
void memberfunctest (int para) {}
static void staticmemfunctest (int para) {}
};
sizeof (B) =?
sizeof (B) = 1
A non-virtual member function of a class is not counted, regardless of whether it is static or not.


Class C
{
C () {}
Virtual ~c () {}
};
sizeof (B) =?
sizeof (B) = 4
Class D has a virtual function, the existence of a virtual function of the class has a one-dimensional virtual function table called virtual table, virtual tables are stored in the virtual function of the address, therefore, the virtual table belongs to the class. The first four bytes of such a class object are a pointer to a virtual table, and the inside of the class must hold the starting pointer of the virtual table. The size of the 32-bit system assigned to the virtual table pointer is 4 bytes, so finally the size of Class C is 4.


Class D
{
D () {}
Virtual ~d () {}
virtual int VirtualMemFuncTest1 () = 0;
virtual int VirtualMemFuncTest2 () = 0;
virtual int VirtualMemFuncTest3 () = 0;
};
sizeof (D) =?
sizeof (D) = 4
Principle of similar C, no matter how many virtual functions within the class, as long as the inside of the class to save the original address of the virtual table, virtual function address can be offset and other algorithms obtained.


Class E
{
int m_int;
Char M_char;
};
sizeof (E) =?
sizeof (E) = 8
The 32-bit operating system int occupies 4 bytes, and char occupies one byte, plus the memory-aligned 3 bytes, which is 8 bytes.


Class F:public E
{
static int s_data;
};
int f::s_data=100;
sizeof (F) =?
sizeof (F) = 8
Why is Class F as big as Class E? The static data member of Class F is placed in a global data member of the program by the compiler, which is one of the members of the class, but 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. The non-static data members of a 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.


Class G:public E
{
virtual int VirtualMemFuncTest1 (int para) = 0;
int m_int;
};
Class H:public G
{
int m_int;
};
sizeof (G) =?
sizeof (H) =?
sizeof (G) = 16
sizeof (H) = 20
You can see that the size of the subclass is the size of the member itself, plus the size of the parent class member. If the parent class also has a parent class, the parent class is also added, so that it continues recursively.


Class I:public D
{
virtual int VirtualMemFuncTest1 () = 0;
virtual int VirtualMemFuncTest2 () = 0;
};
sizeof (I) =?
sizeof (I) = 4
The parent subclass has a virtual function pointer, and the virtual function pointer retains one.

Summarize:
Empty classes also take up memory space, and the size is 1, because C + + requires each instance to have a unique address in memory.
(i) member variables within the class:
Common variables: memory-intensive, but note memory alignment (which is similar to struct type).
Static variable that is statically modified: does not consume memory because the compiler places it in the global variable area.
Variables inherited from the parent class: Calculating into subclasses
(ii) member functions within the class:
Non-virtual functions (constructors, static functions, member functions, and so on): do not consume memory.
virtual function: To occupy a 4-byte (32-bit operating system), to specify the entry address of the virtual function table. is not related to the number of virtual functions. A virtual function pointer is enjoyed by the parent subclass.

The only data that makes up the object itself, no member function is subordinate to any object, and the non-static member function is bound to the object, and the binding mediation is the this pointer. The member function is shared for all objects of the class, not only in simplifying language implementations, saving storage, but also in order to have consistent behavior for homogeneous objects. Homogeneous objects behave as though they are consistent, but manipulate different data members.

The test code is as follows:

[CPP]View Plaincopyprint?
  1. <span style="FONT-SIZE:18PX;" >/*
  2. * File Name:main.cpp
  3. * Description:test the size of C + + ' s class
  4. * Create on:2012-05-31
  5. * Create By:chenchong
  6. * Email: [Email protected]
  7. */
  8. #include <iostream>
  9. Using namespace std;
  10. Class A {};
  11. Class B
  12. {
  13. Public
  14. B () {}
  15. ~b () {}
  16. void memberfunctest ( int para) {}
  17. static void staticmemfunctest ( int para) {}
  18. };
  19. Class C
  20. {
  21. C () {}
  22. Virtual ~c () {}
  23. };
  24. Class D
  25. {
  26. D () {}
  27. Virtual ~d () {}
  28. virtual int VirtualMemFuncTest1 () = 0;
  29. virtual int VirtualMemFuncTest2 () = 0;
  30. virtual int VirtualMemFuncTest3 () = 0;
  31. };
  32. Class E
  33. {
  34. int m_int;
  35. Char M_char;
  36. };
  37. Class F: Public E
  38. {
  39. static int s_data;
  40. };
  41. int f::s_data=100;
  42. Class G: Public E
  43. {
  44. virtual int VirtualMemFuncTest1 (int para) = 0;
  45. int m_int;
  46. };
  47. Class H: Public G
  48. {
  49. int m_int;
  50. };
  51. Class I: Public D
  52. {
  53. virtual int VirtualMemFuncTest1 () = 0;
  54. virtual int VirtualMemFuncTest2 () = 0;
  55. };
  56. int main ( int argc, char **argv)
  57. {
  58. cout<<"sizeof (a) =" <<sizeof (a) <<endl;
  59. cout<<"sizeof (b) =" <<sizeof (b) <<endl;
  60. cout<<"sizeof (c) =" <<sizeof (c) <<endl;
  61. cout<<"sizeof (d) =" <<sizeof (d) <<endl;
  62. cout<<"sizeof (E) =" <<sizeof (e) <<endl;
  63. cout<<"sizeof (f) =" <<sizeof (f) <<endl;
  64. cout<<"sizeof (g) =" <<sizeof (g) <<endl;
  65. cout<<"sizeof (h) =" <<sizeof (h) <<endl;
  66. cout<<"sizeof (i) =" <<sizeof (i) <<endl;
  67. #if defined (_WIN32)
  68. System ("pause");
  69. #endif
  70. return 0;
  71. }
  72. </span>
/* * File Name:main.cpp * Description:test the size of C + + ' s class * Create on:2012-05-31 * Create By:chench ONG * Email: [email protected] */#include <iostream>using namespace Std;class A {}; Class B {public:b () {} ~b () {} void memberfunctest (int para) {} static void staticmemfunctest (int para) {}};CLA SS C {C () {} virtual ~c () {}};class D {D () {} virtual ~d () {} virtual int VirtualMemFuncTest1 () =0; virtual int virtualmemf UncTest2 () = 0; virtual int VirtualMemFuncTest3 () = 0;}; class e{int m_int; char M_char;}; Class f:public e{static int s_data;}; int F::s_data=100;class g:public e{virtual int VirtualMemFuncTest1 (int para) =0; int m_int;}; Class h:public g{int m_int;}; Class i:public d{virtual int VirtualMemFuncTest1 () =0; virtual int VirtualMemFuncTest2 () = 0;}; int main (int argc, char **argv) {cout<< "sizeof (a) =" <<sizeof (a) <<endl; cout<< "sizeof (b) =" <<sizeof (b) <<endl; cout<< "sizeof (C) = "<<sizeof (C) <<endl; cout<< "sizeof (d) =" <<sizeof (d) <<endl; cout<< "sizeof (E) =" <<sizeof (e) <<endl; cout<< "sizeof (f) =" <<sizeof (f) <<endl; cout<< "sizeof (g) =" <<sizeof (g) <<endl; cout<< "sizeof (h) =" <<sizeof (h) <<endl; cout<< "sizeof (i) =" <<sizeof (i) <<endl; #if defined (_WIN32) system ("pause"); #endif return 0;}


Windows 7 32-bit VC 2010 Run Results:

Linux (cent OS 6.2 32-bit) running results:

C + + class occupies memory size calculation

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.