How do I find the size of a class in C + +? and the memory alignment principle (frequently asked questions by interviewers)

Source: Internet
Author: User

<pre name= "code" class= "CPP" > #include <iostream>using namespace std;int main () {class s{};cout<< sizeof (S); return 0;} The program actually outputs 1! What the hell is going on? First I'm here to declare one o'clock--class does not allocate space before initialization, here is the problem of sizeof (class), the following example of the class in C + + in the memory space summary class of memory is determined by the member variables (except static variables), the member function (which is generally said , which will be covered later) is not counted. Excerpt: The member function is also the same as the normal function of existence. A.fun () is called by Fun (A.this). The so-called member function is only nominally in the class. In fact, the size of the member function is not within the object of the class, and multiple objects of the same class share the function code. The member function of our Access class is implemented by a pointer inside the class, which points to the address of each member function recorded in the table,table (although different compilations may have slightly different implementations). So our access to the member function is indirectly obtained by the address. So it adds a certain amount of time overhead, which is why we advocate to declare some short, high-frequency functions as inline (inline functions). (a) class CBase {}; sizeof (CBase) = 1; Why is nothing in the void 1? C + + requires that each instance have a unique address in memory. Pay attention to this sentence!!!!!!!!!! An empty class is also instantiated, so the compiler adds a byte implicitly to the empty class so that the empty class is instantiated with a unique address. So the empty class of sizeof is 1. (ii) class CBase {int A; char p;}; sizeof (CBase) = 8; Remember the alignment problem. int 4 bytes//Note this is very much like the alignment principle of a struct! Char occupies one byte, padded with 3 bytes (c) class CBase {public:cbase (void); virtual ~cbase (void); private:int A; char *p;}; Re-run: sizeof (CBase) The =12c++ class has a virtual function when there is a pointer to the virtual function (VPTR), the 32-bit system assigns a pointer size of 4 bytes. Regardless of how many virtual functions, only this one pointer, 4 bytes. Note that the general function does not have this pointer, andAnd does not account for the memory of the class. (d) class Cchild:public CBase {public:cchild (void); ~cchild (void); virtual void Test ();p rivate:int b;}; Output: sizeof (cchild) = 16; The size of the visible child class is the size of the member variable itself plus the size of the parent class. Part of this is the reason for the virtual function table, be sure to know that the parent class subclass shares a virtual function pointer (v) #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) =1sizeof (b) =1sizeof (c) =4sizeof (d) =8 The first three cases are more common, take note of the fourth case. The size of Class D is more confusing for beginners, Class D is derived from class B,c recently, and its size should be the sum of 5, why is it 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 an integer multiple of the system. and take the nearest rule, which is the most recent multiple, that is the size of the class, so the size of Class D is 8 bytes. Summary: Empty classes are memory-intensive, and the size is 1, because C + + requires each instance to have a unique address in memory. (i) member variables within the class: normal variables: to occupy memory, but be aware of the alignment principle (which is similar to struct type). Static variable that is modified statically: does not consume content because the compiler places it in the global variable area. (ii) member functions inside the class: normal function: does not occupy memory. Virtual function: The entry address of the virtual function table that is to occupy 4 bytes that is used for specifying virtual functions. Therefore, the virtual function of a class occupies a constant address, and the number of virtual functions is not related.


How do I find the size of a class in C + +? and the memory alignment principle (frequently asked questions by interviewers)

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.