C ++ Object Model

Source: Internet
Author: User
I want to summarize the memory usage MECHANISM OF C ++ a long time ago. It is not until now that you have finished the examination and completed this task before you go to the internship.
1. Memory zone used by the program
The memory occupied by a program is generally divided into five types:
(1) Global and static data areas: stores global and static variables (including global and local static variables)
(2) constant data zone: constant strings in the storage program.
(3) code area: the code of the storage program.
(4) STACK: stores automatic variables or local variables and passed function parameters.
(5) Heap: stores dynamically generated data.
When processing the memory, the system automatically alignment the memory, which wastes some memory, but because the CPU runs faster in alignment mode
It is generally good for program performance.
The size of the stack used by a program is fixed and determined by the compiler. It is generally 1 MB.
The stack memory is automatically allocated by the system, and corresponding commands are provided for operation in both the pressure stack and the outbound stack. Therefore, the efficiency is high and the allocated memory space is connected.
Continue, there will be no memory fragments; the memory on the stack is dynamically allocated and recycled by developers. When allocating memory, the system must follow a certain
In the heap space, find a suitable idle heap, modify the linked list to maintain the idle space of the heap, and return the address to the program. Therefore
The rate is lower than the stack, and memory fragments are also easily generated.
From the perspective of the C ++ object model, an object is an area in the memory. If an object is implemented by defining a variable in a function or
When a temporary variable is required for creation, it is an object on the stack; if an object is a variable defined within the global scope, it is to store global/static
State data zone. If an object is created using the new operator, it is an object on the stack.
2. Object Lifecycle
(1) create an object by defining a variable: In this case, the scope of the variable determines the lifecycle of the object. When entering the scope of the variable
Is created. When you exit the scope of the variable, the object is destroyed. It is worth noting that static variables and global variables, because the scope of global variables is integral
Objects declared as global variables are created before the program calls the main () function. After the program exits the main () function, the Global Object
Is destroyed. Static objects are similar to global objects. Although the scope of static variables is not the whole program, static variables are stored in the global/static data zone.
, Allocated at the beginning of the program. Therefore, objects declared as static variables are created when they enter the scope for the first time until they are destroyed when the program exits.
(2) create an object using the new operator: this situation is relatively simple, but it is also the most likely to cause memory leakage. Objects Created through new will always exist,
Until it is deleted. Even if the pointer to the object (generally an automatic variable) has been destroyed, but the delete object has not been called, the object will always
Yes. That is, it occupies the memory space until the program exits, thus causing memory leakage.
(3) object creation through implementation: This generally refers to the creation and destruction of some hidden intermediate temporary variables. They have a short life cycle and are not easy.
Discovered by developers. But it is often the bottleneck that causes program performance degradation, especially for objects that occupy a large amount of memory and are created slowly. These temporary
Objects are generally created using copy constructor. In actual development, parameters are transmitted through value passing, and operators such as ++ and ++ are overloaded to perform arithmetic operations on objects.
During operation, there will also be temporary objects. In these cases, we should try to avoid unnecessary temporary objects.
3. Memory layout of C ++ objects
(1) Non-static data members are the main factors affecting the memory occupied by objects. As the number of objects increases, the memory occupied by non-static data members increases accordingly.
(2) All objects share a static data member. Therefore, the memory occupied by static data members does not increase as the number of objects increases.
(3) static member functions and non-static member functions do not affect the object memory size. Although their implementation occupies the corresponding memory space
The number increases.
(4) If the object contains a virtual function, four bytes of space will be added (virtual function table pointer), no matter how many virtual functions are available.

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.