RPM: Memory partitioning, memory allocation, constant storage, heap, stack, free storage, global zone [c++][memory management] [reprint]

Source: Internet
Author: User
Tags stack pop

Memory partitioning, memory allocation, constant storage, heap, stack, free storage, global zone [c++][memory management] [reprint]

A. In C are divided into these storage areas
1. Stack-the release is automatically assigned by the compiler
2. Heap-usually released by the programmer, if the programmer does not release, the program may end up being recycled by the OS
3. Global zone (Static zone), global variables and static variables are stored in a block, initialized global variables and static variables in an area, uninitialized global variables and uninitialized static variables in another area adjacent. -Program End Release
4. There is also a special place where constants are placed. -Program End Release

The variables defined in the function body are usually on the stack, and the allocated memory functions such as malloc, Calloc, realloc, etc. are allocated on the heap. The global amount is defined in the body of all functions, and after the static modifier is placed in the global zone (static zone), the static variable defined in the body of all functions is valid in the file and cannot be extern to another file. The static representation defined in the function body is valid only within the function body. In addition, a string such as "ADGFDF" in the function is stored in a constant area. Like what:

int a = 0; Global initialization Zone
Char *p1; Global uninitialized Zone
void Main ()
{
int b; Stack
Char s[] = "ABC"; Stack
Char *p2; Stack
Char *p3 = "123456"; 123456{post.content} in the constant area, p3 on the stack
static int c = 0; Global (static) initialization zone
P1 = (char *) malloc (10); Allocate 10 bytes of area in the heap area
P2 = (char *) malloc (20); Allocate 20 bytes of area in the heap area
strcpy (P1, "123456");
123456{post.content} is placed in a constant area and the compiler may optimize it with the "123456" that P3 points to
}


Two. In C + +, memory is divided into 5 zones, which are heap, stack, free storage, global/static storage, and constant storage, respectively.
1. Stack,
is the store of variables that are allocated by the compiler when needed, and that are automatically clear when not needed. The variables inside are usually local variables, function parameters, and so on.
2. Heap,Is the memory blocks allocated by new, their release compiler does not go to the tube, by our application to control, generally a new will correspond to a delete. If the programmer does not release it, the operating system will automatically recycle after the program finishes.
3. Free Storage area,It is the memory blocks allocated by malloc and so on, and he is very similar to the heap, but it uses free to end his life.
4. Global/static storage area, global variables and static variables are allocated in the same piece of memory, in the previous C language, the global variables are divided into initialized and uninitialized, in C + + There is no such distinction, they occupy the same chunk of memory.
5. Constant Storage area,This is a very special storage area, they are stored in a constant, not allowed to modify (of course, you have to pass the improper means can also be modified)

three. Talking about the relationship and difference between heap and stack
Specifically, the modern computer (serial execution mechanism) directly supports the data structure of the stack at the bottom of the code. This is reflected in, there is a dedicated register to the address of the stack, there is a dedicated machine instruction to complete the data into the stack out of the stack operation. This mechanism is characterized by high efficiency, limited data support, generally integer, pointer, floating point and other systems directly supported by the data type, does not directly support other data structures. Because of this feature of the stack, the use of stacks is very frequent in the program. The call to the child program is done directly using the stack. The call instruction of the machine implies that the return address is pushed into the stack and then jumps to the subroutine address, while the RET instruction in the subroutine implicitly pops the return address from the stack and jumps to the operation. The automatic variable in C + + is an example of using the stack directly, which is why when the function returns, the function'sAutomatic variable automatic invalidationThe reason.

Unlike stacks, the data structure of a heap is not supported by the system (whether it is a machine system or an operating system), but is provided by a library of functions. The basic Malloc/realloc/free function maintains a set of internal heap data structures. When the program uses these functions to obtain new memory space, the function first tries to find the available memory space from the internal heap, if there is no memory space available, then attempts to use the system to dynamically increase the size of the program data segment memory, the newly allocated space is first organized into the internal heap, It is then returned to the caller in the appropriate form. When the program frees the allocated memory space, this memory space is returned to the internal heap structure and may be appropriately processed (such as merging additional free space into a larger free space) to better fit the next memory allocation request. This complex allocation mechanism is actually equivalent to abuffer pool for memory allocation(Cache), there are several reasons for using this set of mechanisms:
1. System calls may not support memory allocations of any size. Some system calls only support a fixed size and multiple memory requests (per page allocation), which can be wasteful for a large number of small memory classifications.
2. System calls to request memory can be costly. System calls may involve conversion of the user state and kernel mentality.
3. Memory allocations that are not managed can easily cause memory fragmentation under the allocation release operation of a large amount of complex memory.

comparison of heaps and stacks
From the above knowledge, the stack is the function provided by the system, the characteristics are fast and efficient, the disadvantage is limited, the data is not flexible, and the stack is the function library provides the function, the characteristic is flexible and convenient, the data adapts widely, but the efficiency has certain reduction. A stack is a system data structure that is unique to a process/thread, and a heap is a library internal data structure that is not necessarily unique. Memory allocated by different heaps cannot operate with each other. The stack space is divided into two kinds: static allocation and dynamic allocation. Static allocations are done by the compiler, such as the automatic variable (auto) assignment. Dynamic allocation is done by the Alloca function. The dynamic allocation of the stack does not have to be released (it is automatic), and there is no release function. For portable programs, the dynamic allocation of stacks is not encouraged! The allocation of heap space is always dynamic, although all data space will be released back to the system at the end of the program, but an accurate memory/release memory match is an essential element of a good program.

1. Fragmentation problem: for heaps,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, detailed can > reference data structure, Here we are no longer one by one discussion.
2. Direction of growth: for the heap, the direction of growth is upward, that is, to the memory address of the direction of increase, for the stack, its growth direction is downward, is toward the memory address of the direction of growth.
3. Allocation method: The heap is dynamically allocated and there is no statically allocated heap. 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.
4. Allocation efficiency: The stack is the data structure provided by the machine system, the computer will support the stack at the bottom: assigning the address of the special register storage stack, the stack stack has the special instruction execution, which decidesthe efficiency of the stack is relatively 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.

clearly differentiate stacks from stacks:
On BBS, heap and stack of the problem, seems to be an eternal topic, this shows that the beginner is often confused, so I decided to take his first surgery.
First, let's give an example:

void F ()
{
int* P=new int[5];
}

This short sentence contains the heap and stack, see new, we should first think that we have allocated a heap of memory, then the pointer p? He allocates a stack of memory, so the meaning of this sentence is:a pointer to a heap of memory is stored in the stack memory p。 The program will first determine the size of the memory allocated in the heap, then call operator new to allocate memory, and then return the first address of the memory, put in the stack, his assembly code under VC6 is as follows:
00401028 Push 14h
0040102A call operator new (00401060)
0040102F Add esp,4
00401032 mov dword ptr [Ebp-8],eax
00401035 mov eax,dword ptr [ebp-8]
00401038 mov dword ptr [Ebp-4],eax
Here, we do not release the memory for the sake of simplicity, then how to release it? Is it delete p? O, wrong, it should be.Delete []p, this is to tell the compiler that I'm deleting an array, and that VC6 will do the work of releasing the memory based on the corresponding cookie information.
Well, let's go back to our topic: What's the difference between heaps and stacks?
The main differences are from the following points:
1, different management methods;
2, the space size is different;
3, can produce different fragments;
4, the growth direction is different;
5, the distribution method is different;
6, the allocation efficiency is different;
Management mode: For the stack, is automatically managed by the compiler, without our manual control, for the heap, the release of work by the programmer control, easy to produce memory leak.
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, under the VC6, the default stack space is 1M (as if it is not clear). Of course, we can modify:
Open the project and proceed to the following menu: Project->setting->link, select output in category, then set the maximum and commit of the stack in the reserve.
Note: The reserve minimum value of 4byte;commit is reserved in the virtual memory of the page file, it is set to a large size stack will open up a larger value, may increase the memory overhead and startup time.
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. So, we recommend that you try to use stacks instead of heaps.

In addition, the comparison of access efficiency:
Code:

Char s1[] = "AAAAAAAAAAAAAAA";
Char *s2 = "BBBBBBBBBBBBBBBBB";

AAAAAAAAAAA is assigned at run time;
And BBBBBBBBBBB is determined at compile time;
However, in subsequent accesses, the array on the stack is faster than the string that the pointer points to (for example, a heap) .
Like what:

void Main ()
{
char a = 1;
Char c[] = "1234567890";
Char *p = "1234567890";
A = c[1];
A = p[1];
Return
}

The corresponding assembly code
10:a = c[1];
00401067 8A 4D F1 mov cl,byte ptr [ebp-0fh]
0040106A 4D FC mov byte ptr [ebp-4],cl
11:a = p[1];
0040106D 8B-EC mov edx,dword ptr [ebp-14h]
00401070 8A mov al,byte ptr [edx+1]
00401073 FC mov byte ptr [ebp-4],al
The first reads the elements in the string directly into the register CL, while the second one reads the pointer values into EDX, which is obviously slow to read the characters according to EdX.
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, It's important to write code that is stable and secure when it's time to collapse.

RPM: Memory partitioning, memory allocation, constant storage, heap, stack, free storage, global zone [c++][memory management] [reprint]

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.