On the C ++ static memory usage ------ Vs Vs dynamic heap stack of smart pointers

C ++ Memory Overview

  In C ++, memory is divided into five areas: heap, stack, free store, static / global storage area and the constant storage area.

  • Static memory: static memory used to store a local static objects, classes, static data members, and define a variable outside of any function (global variable).
  • Stack Memory: stack memory used to hold non-static objects within a defined function.
  • Heap memory: memory using the new keyword open space is located in the heap memory, programmers have released (not by the compiler is responsible), a new general corresponds to a delete, a new [] corresponds to a delete [], if the programmer is not released, at the end of the program, the system will automatically recover the resources.
  • Free store: Yes * alloc allocated memory space that is located in free space, use free to release
  • Constant storage area: a special storage area, which kept constant, it can not be modified.
      Heap and free space is actually a whole memory pool, which belongs to the same area, new underlying implementation calls malloc, new advanced version of malloc can be seen as intelligent.

VS stack heap

  Heap memory and stack memory is the memory of two different areas, they both differ in many ways there are to contact:

  • Management:
    • Heap of resources to control programmer, sometimes we forget to free memory, can cause a memory leak (memory leak); sometimes a data memory unit still pointer references, we just put the memory, it will produce a valid reference memory pointer.
    • Stack resources managed automatically by the compiler, without manual control.
  • System response:
    • Stack for the stack, the system has a record pointer free memory address, the application program when the system receives, traverse the linked list, the first node to find a stack space is larger than the application space, remove a node from the free list, and be assigned to the program. If the size of the block of heap memory is exactly equal to the size of the application space, the first address is directly returned to the user; if the memory block size larger than the requested size, the block splitting this, the remaining portion of the heap memory remaining available heap memory in the form of "heap linked list" and other memory related unassigned. If a block of heap memory is not found to meet the size of the whole heap list, the system will give "heap linked list" link a larger area for its use, if this step is still failure, malloc will return NULL, and programmers error.
    • Stack , as long as the remaining stack space is greater than the application space, the system provides memory for the program, otherwise it reported an exception in the stack overflow.
  • size of space
    • Heap : heap memory area is not continuous, since the system uses a free space to store the linked list, heap size is limited by the active computer system virtual memory (32-bit system theoretically 4G), so that the stack space is relatively flexible, is relatively large.
    • Stack : The stack is a contiguous memory area, the predetermined size is a good operating system, is determined on a compile-time constant. If the application stack space than the remaining space will be prompted to overflow. Therefore, less space obtained from the stack.
  • Debris
    • Heap : heap for frequent use new / delete will cause a lot of debris, reduce the efficiency of the program.
    • Stack : For the stack, the latter is advanced out of the queue, and out of correspondence, no debris.
  • Growth direction
    • Stack : the stack upwardly extended from the lower to higher addresses.
    • Stack : Stack downwardly, extended from the upper address to the lower address.
  • Allocative efficiency
    • Heap : heap that are dynamically allocated by the memory allocated by new, usually slow and prone to debris, not easy to use.
    • Stack : Stack is automatically assigned by the system, there are both dynamically allocated statically allocated. Faster, but the programmer can not control.

malloc and free

malloc

  According to the definition of standard C library function, malloc has the following prototype:

void* malloc(size_t size);

To achieve this function distribution function is a continuous period of available memory in the system, specifically:

  • Malloc allocated memory size of at least the number of bytes specified by the size parameter.
  • malloc return value is a pointer to the starting address of the memory section available.
  • Multiple calls to malloc allocated addresses should not overlap, unless a particular malloc assigned address is released.
  • malloc memory allocation should be completed as soon as possible and return (without the use of memory allocation algorithms are NP-hard).
  • Malloc should achieve to achieve resizing and deallocation of memory function (realloc, free)

free

  free function is used before releasing the call malloc, calloc, realloc the allocated memory space, has the following prototype:

void free(void *ptr);

  free implementation not look so simple, to be free, we must first address two key issues:

  • 1. How to verify that incoming address is valid, that is determined to be the first address allocated by malloc way data area. To solve this problem, first determine the address before the change malloc allocated region, i.e. in the range first_block pointer and the current break; secondly before the address actually allocated by malloc.
  • 2. How to solve the fragmentation problem.

new sum delete

  In C ++, the dynamic memory management division is accomplished by a pair of operators. new: in dynamic memory space allocated for the object and returns a pointer pointing to the object, we can choose to initialize the object. delete: to accept a dynamic object pointer, destroy the object, and frees the memory associated with it.
The difference between malloc and new

  • malloc / free standard library function, new / delete operators in C ++;
  • malloc failed to return empty, new failure throws an exception;
  • new / delete calls the constructor, destructor, malloc / free do not, so what he can not meet the requirements of dynamic objects.
  • It returns a pointer with a new type, malloc returns untyped pointer.

Smart Pointers

  To make it easier and safer to use dynamic memory, C ++ standard library provides a new smart pointer types to manage dynamic objects. It is similar to the normal pointer, an important difference is that it is responsible for automatically releasing objects referred to. Because the smart pointer is a class, when the class goes out of scope, the class destructor will automatically call the destructor will automatically release resources. So the role of the principle of smart pointer is automatically free up memory space at the end of the function, without the need to manually release the memory space.

  • shared_ptr allows multiple pointers to the same object, the object repeat itself relevant resource will be released in the "last reference was withdrawn" when. It uses the counting mechanism to indicate that resources are shared several pointers, can be used () to see the owner of the resource by the number of member functions use_count.
  • unique_ptr achieve exclusive ownership or have strict concept, ensure that only one smart pointer can point to the object in the same time. Particularly useful to avoid resource leaks
  • weak_ptr is a smart pointer does not control the life cycle, it points to an object managed by shared_ptr. Be the object memory management is strong references shared_ptr, weak_ptr only provides a means of access to managed objects. Object of the design is to fit weak_ptr shared_ptr smart pointer introduced to assist in shared_ptr work, it can only be from one or the other weak_ptr shared_ptr object construction, and his structure destructor does not cause an increase or decrease in the count. weak_ptr is used to solve problems when thinking of shared_ptr refer to each other, if two shared_ptr refer to each other, then the two pointer reference count never dropped to 0, the resource is never released.
Released seven original articles · won praise 2 · Views 498

Guess you like

Origin blog.csdn.net/NewB20143864/article/details/104797261