C ++ Object Model (1): Introduction to object memory Layout

Source: Internet
Author: User

We know that memory space needs to be allocated when a class is instantiated, that is, when an object is generated. How many bytes of memory space does an object need? What rules are used for calculation?

 

For example

Bytes ------------------------------------------------------------------------------------------------------------------------------

Class declaration Arwen. h

Class Arwen

{

Public:

Arwen (void );

Virtual ~ Arwen (void );

 

Int age ();

Staticvoid
Height (INT );

PRIVATE:

Int m_nage;

Staticint
S_nheight;

};

Certificate ----------------------------------------------------------------------------------------------------------------------------------

Class source file Arwen. cpp

# Include "Arwen. H"

Int Arwen: s_nheight = 1; // remember to initialize static variables.

Arwen: Arwen (void ){}

Arwen ::~ Arwen (void ){}

 

Int Arwen: Age ()

{

Return m_nage;

}

Void Arwen: height (int
A)

{

S_nheight =;

}

Bytes -----------------------------------------------------------------------------------------------------------------------

 

If we instantiate the above class, how does one allocate space in the memory?

We know that a class includes data members and member functions ).

Data members are classified into static and nonstatic

Member functions include static, nonstatic, and virtual

 

Data member Memory Allocation

Static variables are used to allocate space in the static storage area (also called data segments), while nonstatic variables are used to allocate space in objects.

So static
Int s_nheight; space allocated in the static storage area, not included in the calculation of the object space. Int m_nage; 4 bytes space is required

 

Member Function Memory Allocation

Both static and nonstatic member functions allocate memory in the code area (or text segment) and are not included in the calculation of object content.

Virtual member functions (no matter how many) ONLY insert a pointer to an object and point to a virtual table (where is this stored? Not yet understood), the virtual table contains a bunch of pointers, a pointer pointing to type information, and others pointing to the virtual function address. virtual functions are stored in the code area like static and nonstatic member functions.

Since Arwen class has virtual functions, all compilers will automatically insert a pointer pointing to the virtual table, and the pointer is 4 bits on the 32-bit system.

 

 

Therefore, Arwen of the instantiation class needs to allocate 8 bytes of memory space. Four bytes of int m_nage plus four bytes of pointer to the virtual table.

You can use sizeof (Arwen) to calculate and find that the result is 8.

 

However, in some special cases, the result may not be the calculated value. for example, if a class does not have any data member or virtual member function, it is 0, but sizeof finds that it will be 1, because it is the only identifier of a class to use in the memory. in addition, sometimes the calculated value is 7 or another integer multiple of what is not 2. Because of the memory object mechanism (it may be aligned according to the member data and the overall alignment ), the final sizeof results may be different.

Memory alignment see: http://baike.baidu.com/view/4786260.htm

 

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.