Abstract: "new" is a keyword of C++ and also an operator. There are many topics about new, because it is really complicated and very mysterious.

This article is shared from the HUAWEI cloud community "How to write efficient, elegant, and credible code series you really use new 160e549ef4295c", the original author: I am a big watermelon.

C++ memory management

1. C++ memory allocation

After the program in C++ is loaded into the memory, it is laid out according to the code area, data area, heap area, and stack area. The data area can be divided into free storage area, global/static storage area and constant storage area. The length of each area is as follows:

  • Stack area
    When the function is executed, the storage units of the local variables are created on the stack, and the storage units are automatically released after the function is executed. stack memory allocation operation is built in the processor instruction set, which is efficient, but the allocated memory capacity is limited.
  • Heap area
    The heap is the new memory block, regardless of the compiler release, by the application control, new corresponds to delete . If it is not released, the operating system will automatically recycle it after the program ends.
  • Free storage area
    The memory block allocated by malloc in C. End the life cycle with free.
  • Global/Static Storage Area
    Global variables and static variables are allocated to the same block of memory, and will be initialized when
  • Constant storage area
    The special storage area stores constants and cannot be modified.

The difference between heap and stack

  • Management method
    The stack is automatically managed by the compiler, and the heap is controlled by the programmer
  • size of space
    In a 32-bit system, the heap memory can reach 4GB, and the stack has a certain amount of space
  • Fragment management
    For the heap, frequent new/delete will definitely cause the discontinuity of the memory space and generate a large number of memory fragments to reduce the efficiency of the program; the stack follows the first-in-last-out rule, so there will be no gaps.
  • Growth direction
    The heap grows upwards, that is, it grows in the direction of increasing memory addresses; while the stack grows in the direction of decreasing memory addresses.
  • Allocation
    The heap is dynamically allocated. The stack is divided into dynamic allocation and static allocation: static allocation is done by the compiler, and dynamic allocation is done by the alloca function. Even if it is dynamically allocated, it is still automatically released by the compiler.
  • Distribution efficiency
    The bottom layer of the computer provides the support of the stack, allocates special registers to store the address of the stack, and pushes and pops the stack to have special instructions to execute, which determines the efficiency of the stack will be higher. The heap is provided by the C/C++ function library, and the mechanism is more complicated. For example, in order to allocate a certain size of memory, it is necessary to search for a sufficient amount of space in the heap memory, and the efficiency is much lower than that of the stack.

2. new/delete and new []/delete []

  • Use delete when reclaiming the memory space of a single object allocated by new, and delete[] when reclaiming a group of objects allocated by new[]
  • For built-in types (int/double/float/char/...), when new[] applies for memory, the compiler will quietly store integers in memory, indicating the number of pointer arrays, so delete/delete[] can be correct Release the requested memory space
  • It is recommended to use [] when calling new, then use [] when calling delete

3. Three forms of new

  • new operator commonly used new operator has built-in language functions and cannot be overloaded. There are three things that are actually done during the call:
  • Allocate memory for type objects;
  • Call the constructor to initialize the memory object;
  • Return object pointer
    If you are creating an object on the heap, use the new operator directly.
  • operator new ordinary operator, can be overloaded. If it is just to allocate memory, then operator new should be called, but it is not responsible for initialization. The allocator provided by the system by default has some problems in both time and space: the speed of the allocator is slow, the space waste is serious when allocating small objects, and overloading new/delete has three benefits:
  • Improve efficiency
  • Detect memory errors in the code
  • Get statistics on memory usage
  • The C++ standard stipulates that the overloaded operator new must be a class member function or a global function. The global operator new overload should not change the original signature, but directly and seamlessly replace the original version. Global overloading is very aggressive. Others cannot use the default new when using your library. The overloading of specific classes will only affect this class and its derived classes. However, the overload of the operator new function of the class must be declared as static, because operator new is called before the concrete object of the class is constructed.
  • In order to obtain the advantages of 2 and 3, the overloaded operator new needs the following function declaration void operator new(size_t, const char file, int line);
  • placement new defined in the library <<new>>. If you want to create an object in a piece of already acquired memory, you should call placement new. Generally not recommended, but some time very demanding applications be considered, because the choice of appropriate constructor object initialization completion time is a relatively long process.

Click to follow and learn about Huawei Cloud's fresh technology for the first time~


华为云开发者联盟
1.4k 声望1.8k 粉丝

生于云,长于云,让开发者成为决定性力量