Basic concepts of C ++ memory usage Mechanism

Source: Internet
Author: User

In the C ++ programming language, memory operations are a very important application technology. As a programmer, you must be aware of this application. Here we will first introduce the concepts related to the C ++ memory usage mechanism, hoping to help you.

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, therefore, 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 continuous without generating memory fragments. The memory on the stack is dynamically allocated and recycled by developers. When allocating memory, the system needs to find a suitable idle heap in the heap space according to a certain algorithm, modify the linked list to maintain the idle space of the heap, and then return the address to the program. Therefore, the efficiency is lower than the stack, and memory fragments are also easily generated.

From the perspective of the C ++ object model, the object is an area in the C ++ memory usage mechanism. If an object is created by defining a variable in a function or implementing a required temporary variable, it is an object on the stack. If an object is a variable defined within the global scope, it is the global/static data storage area. 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. The object is created when it enters the scope of the variable. When you exit the scope of the variable, the object is destroyed. It is worth noting that static variables and global variables are created before the program calls the main () function because the scope of global variables is the whole program. 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 area and 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 exist until they are 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 exist. 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 easily noticed 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 through copy constructor. In actual development, there will also be temporary objects when performing arithmetic operations on objects by passing values, overloading ++ and other operators, 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 C ++ memory usage mechanism space, it also does not increase with the increase in the number of objects.

(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.