C + + Memory Management Learning notes (1)

Source: Internet
Author: User
Tags stack pop

/****************************************************************/

/* Learning is a collaborative and shared style!

/* Author:atlas Email:[email protected]

/* welcome> Xiao's blog: zhangxiaolong.org

* * Reprint Please indicate the source of this article:

* http://blog.csdn.net/wdzxl198/article/details/9057695

/****************************************************************/

a C + + memory management1. How to allocate memory

Before explaining the memory allocation, first of all, to understand what the program is in the storage area, and then in a detailed analysis of various allocation methods.

1.1 C language and C + + memory allocation area

In the following three graphs, Figure 1, Figure 2, is a more detailed C-Language memory area division method. Figure 3 is a typical C + + memory map, easy to understand, the following memory allocation diagram, the difference is that figures 1 and 2 are divided into initialization and uninitialized static variable area, Figure 3 is the global variable area.

C language (Figure 1 and Figure 2): (From address to high address)

A) body segment: Used to store program execution code. Normally, the body segment is shareable. In addition, the body segment is often read-only, preventing the program from accidentally modifying itself.

b) Initialize the data segment: used to store the initialized global variables in the program. The data segment belongs to static memory allocation.

c) Non-initialized data segments: typically called BSS segments, which are used to hold uninitialized global variables in the program. BSS is the abbreviation for the English block Started by symbol (a block starting with a symbol). BSS segments belong to static memory allocations. The kernel initializes the data in this segment to 0 or a null pointer before the program begins execution.

d) Heap: The heap is used to store dynamically allocated memory segments in the process, which are not fixed and can be dynamically expanded or scaled down. When a process calls a function such as malloc/free to allocate memory, the newly allocated memory is dynamically added to the heap (heap is expanded)/freed of memory from the heap (heap is scaled down).

e) Stacks: Stacks are also called stacks, which store local variables of the program (but do not include variables of static declarations, static means that variables are stored in the data segment). In addition, the stack is used to pass parameters and return values when a function is called. Due to the advanced post-out features of the stack, the stack is particularly handy for saving/recovering call sites.

Figure 1 Typical C-language memory distribution Area (Unix advanced Environment programming) Figure 2 typical C-language memory distribution area

C + + (Figure 3):

According to C + + memory management technology Insider, in C + +, memory is divided into 5 zones, each of which is a heap, stack, free survival Zone, global/static survival zone, and constant survival area.

A) stack: the memory is automatically allocated and freed by the compiler when needed. Typically used to store local variables and function parameters. The stack operation allocation is built into the processor's instruction set, which is highly efficient, but allocates limited memory capacity.

b) Heap: Memory is allocated using new for allocation using DELETE or delete[] release. Failure to properly release memory can result in a memory leak. But at the end of the program, it is automatically reclaimed by the operating system.

c) Free storage: Use malloc for allocation, and then use to reclaim. Similar to the heap.

d) Global/static storage: Global variables and static variables are allocated in the same piece of memory, C language is distinguished from initialization and uninitialized, C + + is no longer differentiated.

e) constant storage: Stores constants and is not allowed to be modified.

Here, in some of the data is the definition of C + + memory allocation, programmable within the existence of basically divided into such a few parts: static storage, heap and stack area. They have different functions and different ways of using them.

A) static storage: Within the time the program is compiled, it has been allocated, and the entire running period of the program exists in this block. It mainly stores static data, global data, and constants.

b) Stack: When executing a function, the storage units of local variables within the function can be created on the stack and are automatically freed when the function is executed. The stack memory allocation operation is built into the processor's instruction set and is highly efficient, but allocates limited memory capacity.

c) Heap area: Also known as dynamic memory allocation. The program uses malloc or new to request any size of memory at run time, and the programmer is responsible for freeing the memory with free or delete when appropriate. The lifetime of dynamic memory can be determined by us, and if we do not release the memory, the program will release the dynamic memory at the end. However, good programming habits are: If a dynamic memory is no longer used, it needs to be released, otherwise, we think there is a memory leak phenomenon.

Figure 3 Typical C + + memory region

Summary: C + + and C language memory allocation is different, but the overall consistency, will not affect the program analysis. In the case of C + +, whether it is 5 or 3, the division is inconsistent, and (c) d) E in 5) (Part A) is 3.

1.2 distinguish heap, stack, static storage area

Let's take a look at the code snippet to see how the three parts of memory need to be manipulated and different, and where to pay attention.

(1) static storage area and stack area

1:char*>
2:char>
3:>
4:>
   5:char*>

This program is error, error occurs in p[2] = ' A ' This line of code, for what, is the variable p and variable array A all exist in the stack (any temporary variables are in the stack, including the variables defined in the main () function). However, the data "Hello World1" and the data "Hello World2" are stored in different regions.

Because the data "Hello World2" exists in the array, this data is stored in the stack area, and there is no problem with its modification. Because the pointer variable p is only able to store the address of a storage space, the data "Hello World1" is a string constant, so it is stored in a static storage area. Although through p[2] you can access the third unit of data in the static store, the unit of storage where the character ' L ' resides. However, because the data "Hello World1" is a string constant, it cannot be changed, so a memory error is reported when the program is run. Also, if the output of P and P1 at this time, it will be found that the address stored in P and P1 is exactly the same. In other words, only one copy of the same data is kept in the data area.

(2) heap and stack differences

Let's start with an example to visually illustrate the difference between stack and heap memory, and then analyze the case in detail in Example 2.

Example 1:
   1:VOID fn () {
   2:     int* p = new Int[5];
   3:}

To see new, we should first think that we allocated a heap of memory, then the pointer p? It allocates a stack of memory, so the meaning of this sentence is: In the stack memory is stored in a pointer to a heap of memory p. The program determines the size of the memory allocated in the heap, then calls operator new to allocate memory, then returns the first address of the memory and puts it into the stack.

Note: In order to simply not release the memory, then how to release it? Is it deletep? NO, wrong, it should be delete [] p, which is telling the compiler: the deletion is an array.

Example 2:

   1:int>//Global Initialization Zone
   2:char *p1; Global uninitialized Zone
   3:main ()
   4: {
   5:     int b;  Stack
   6:     char> "abc";//Stack
   7:     char *p2;//Stack
   8:     Char *p3 = "123456";   123456\0 in the constant area, p3 on the stack.
   9:     static int c =0;     //Global (Static) initialization area
  Ten:     p1 = (char *) malloc (10);
  One:     p2 = (char *) malloc (20);
  The         area of 10 and 20 bytes allocated is in the heap area.
       strcpy (P1, "123456");//123456\0 is placed in a constant area, the compiler may optimize it with the "123456" pointed to by P3 as a place.
  14:}

Example 3:

   1:char* F1 ()  
   2: {  
3:char*>
   4:char A;  
5:>
   6:return p;  
   7:}  
   8:char* F2 ()  
   9: {  
10:char*>
  11:p = (char*) new char[4];  
  12:return p;  
  

Both of these functions return the address of a storage space, what is the difference between them? The F1 () function returns a storage space, but this space is a temporary space. In other words, this space has only a short life cycle, and its life cycle at the end of the function F1 () call loses its life value, namely: this space is freed. So, when you call the F1 () function, if you have the following statement in your program:

   1:char* p;  
2:>
   3: *p = ' a ';
at this point, the compilation does not report an error, but an exception error occurs when the program is run. Because you're working on memory that should not be manipulated (that is, the storage space that has been freed). However, in contrast, the F2 () function does not have any problems. Because the new command is to request storage space in the heap, once the application succeeds, the memory will persist unless you delete it or terminate the program. It can also be understood that heap memory is a shared unit that can be accessed by multiple functions together. Heap memory is a good choice if you need to have more than one data return but no way. But be sure to avoid the following things happening:

   1:void F ()  
   2: {  
   3: ...  
   4:char * p;  
   5:p = (char*) new char[100];  
   6: ...  
   

This program does a very pointless thing and can bring great harm. Because, although the heap memory is applied, p holds the first address of the heap memory. However, this variable is a temporary variable and the p variable disappears when the function call ends. That is, there is no more variable to store the first address of this heap of memory, and we will never be able to use that heap of memory again. However, this heap of memory has always been identified by your use (because you did not delete it until the end of the program, so this heap of memory has been identified by the owner as the current program), and other processes or programs are not available. We refer to this unethical "rogue behavior" (which we don't use, but do not allow others) to call a memory leak (leak).

Combined with the above two examples, we can summarize what is the difference between heap and stack!

(1) different management methods

For the stack, it is automatically managed by the compiler, without our manual control, and for the heap, the release work is controlled by the programmer and easily generates memory leak.

(2) different space sizes

Space size: Generally speaking, in 32-bit system, heap memory can reach 4G space, from this point of view heap memory is almost no limit. But for the stack, generally there is a certain amount of space, for example, in the VC6.0 below the default stack space size is 1M, you can modify this value.

(3) can produce different fragments

For the heap, frequent new/delete is bound to cause memory space discontinuity, resulting in a large number of fragments, so that program efficiency is reduced. For the stack, there is no problem, because the stack is advanced out of the queue, they are so one by one correspondence, so that there will never be a memory block from the middle of the stack pop up, before he pops up in the back of his upper stack content has been ejected.

(4) different growth direction

For the heap, the direction of growth is upward, that is, the direction of the memory address increase, for the stack, its growth direction is downward, is the direction of the memory address is reduced to grow. (See the memory allocation diagram for the first part)

(5) different distribution methods

The heap is dynamically allocated and there are no statically allocated heaps. Stacks are allocated in 2 ways: static allocation and dynamic allocation. Static allocations are done by the compiler, such as the allocation of local variables. The dynamic allocation is assigned by the ALLOCA function, but the dynamic allocation of the stack is different from the heap, and his dynamic allocation is released by the compiler without our manual implementation.

(6) Different allocation efficiency

The stack is the data structure provided by the machine system, the computer will support the stack at the bottom: allocate the address of the special register storage stack, the stack stack has special instruction execution, which determines the efficiency of the stack is high. The heap is provided by C + + function library, its mechanism is very complex, for example, in order to allocate a piece of memory, the library function will follow a certain algorithm (the specific algorithm can refer to the data structure/operating system) in the heap memory to search for available enough space, if there is not enough space (possibly due to too much memory fragmentation), It is possible to invoke the system function to increase the memory space of the program data segment, so that there is a chance to divide the memory in sufficient size and then return. Obviously, the heap is much less efficient than the stack.

Summarize:

Heap and Stack, due to the use of a large number of new/delete, prone to large amounts of memory fragmentation, due to lack of dedicated system support, inefficient, due to the possibility of triggering user-state and nuclear mentality of the switch, memory applications, the cost becomes more expensive. So the stack is the most widely used in the program, even if the call of the function is done by the stack, the parameters in the function call, the return address, the EBP and the local variables are all stored in a stack. Therefore, we recommend that you try to use stacks instead of heaps.

Although the stack has so many advantages, it is not so flexible compared to the heap, sometimes allocating a lot of memory space, or heap better.

Whether it is a heap or stack, to prevent the occurrence of cross-border phenomenon (unless you deliberately make it out of bounds), because the result of the cross-border is either a program crash, or destroy the program heap, stack structure, produce unexpected results, even in the course of your program, did not occur above the problem, you still have to be careful, When it's time to blow up, it's pretty hard to debug.

Reference: C + + memory Management Learning outline: http://blog.csdn.net/wdzxl198/article/details/9050587

edit:2013.6.8 16:42

Atlas

C + + Memory Management Learning notes (1)

Related Article

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.