Heap space, stack space, and stack balance

Source: Internet
Author: User

Stack and stack differences
I. prerequisites-program memory allocation
The memory occupied by a C/C ++ compiled program is divided into the following parts:
1. STACK: the stack zone is automatically allocated and released by the compiler, and stores function parameter values and local variable values. The operation method is similar to the stack in the data structure.
2. Heap-generally assigned and released by the programmer. If the programmer does not release the heap, it may be recycled by the OS at the end of the program. Note that it is different from the heap in the data structure. The allocation method is similar to the linked list.
3. Global (static)-the storage of global variables and static variables is put together, and the initialized global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. -The system is released after the program ends.
4. Text Constant Area-constant strings are placed here. The program is released by the System
5. program code area-stores the binary code of the function body.

 

System Response after application
STACK: as long as the remaining space of the stack exceeds the applied space, the system will provide the program with memory. Otherwise, an exception will be reported, prompting stack overflow.
Heap: First, you should know that the operating system has a linked list that records idle memory addresses. When the system receives a program application,
Yes
Traverse the linked list to find the heap node with the first space greater than the requested space, delete the node from the idle node linked list, and allocate the space of the node to the program, for most systems
The size of the allocation is recorded at the first address in the memory space. In this way, the delete statement in the code can correctly release the memory space. In addition, the size of the heap node is not necessarily equal to the size of the application.
Small, the system will automatically put the excess part into the idle linked list.

2.3 Application size limit
STACK: in windows, the stack is a data knot that expands to a low address.
Is a continuous area of memory. This sentence means that the address at the top of the stack and the maximum stack capacity are pre-defined by the system. In Windows, the stack size is 2 MB (OR 1 MB ).
A constant determined during compilation). If the requested space exceeds the remaining space of the stack, overflow is displayed. Therefore, the space available from the stack is small.
Heap: the heap is a data structure extended to the high address and a non-sequential memory area. This is because the system uses the linked list to store the idle memory address, which is naturally discontinuous, And the traversal direction of the linked list is from the low address to the high address. The heap size is limited by the valid virtual memory in the computer system. It can be seen that the space obtained by the heap is flexible and large.

 

2.6 comparison of access efficiency

Char S1 [] = "aaaaaaaaaaaaa ";
Char * S2 = "bbbbbbbbbbbbbbbbb ";
Aaaaaaaaaaa is assigned a value at the runtime;
Bbbbbbbbbbbbb is determined during compilation;
However, in future access, the array on the stack is faster than the string pointed to by the pointer (such as the heap.
For example:
# Include
Void main ()
{
Char A = 1;
Char C [] = "1234567890 ";
Char * P = "1234567890 ";
A = C [1];
A = P [1];
Return;
}
Corresponding assembly code
10: A = C [1];
00401067 8A 4D F1 mov Cl, byte PTR [ebp-0Fh]
0040106a 88 4D FC mov byte PTR [ebp-4], Cl
11: A = P [1];
0040106d 8B 55 EC mov edX, dword ptr [ebp-14h]
00401070 8A 42 01 mov Al, byte PTR [edX + 1]
00401073 88 45 FC mov byte PTR [ebp-4], Al
The first type reads the elements in the string directly into the CL register, while the second type reads the pointer value into EDX. Reading the characters based on edX is obviously slow.

2.7 summary:
The difference between stack and stack can be seen in the following metaphor:
Using Stacks is like eating at a restaurant, just ordering food (sending an application), paying for it, and eating (using it). If you are full, you can leave, without having to worry about the preparation work, such as cutting and washing dishes, as well as the cleaning and tail work such as washing dishes and flushing pots, his advantage is fast, but his degree of freedom is small.
Using heap is like making your favorite dishes. It is troublesome, but it suits your taste and has a high degree of freedom.

 

Stack and stack


Heap Space
Of
Memory Allocation is user-level memory allocation. The allocated memory must be manually released; otherwise, memory leakage may occur. Of course, the application can also be released when it is destroyed. In a 32-bit system, the heap size is reachable.
4 GB. It can be seen that the heap space can be very large. However, frequent heap allocation and release may result in memory fragmentation, or cause kernel mode and user mode (Unix/Linux Kernel Mode and user mode) frequency.
Frequent switching and memory application results in performance loss. For example, in order to allocate a piece of memory, C/C ++ library functions follow certain algorithms.
In
Search in heap memory to check whether there is enough space available. If there is not enough space (probably because there are too many memory fragments), it is possible to increase the memory space of the program data segment by executing system calls.
In this way, you have the opportunity to allocate enough memory and then return. However, we need to see that the heap provides us with enough flexibility for memory usage. Especially in some cases, the dynamic allocation of memory for us
Greatly saves memory space.


Stack space
Of
Memory Allocation is managed by the system. It is system-level memory allocation, which generally does not require manual control. Local variables, return addresses of called subprograms, and register information are stored in the stack. We
It is known that the stack is a FIFO (first-in-first-out) structure, so the memory on the stack space is continuously occupied, and there will be no memory fragments. However, the stack size is limited.


For example
Below, the default stack space is
1 m
. Of course, you can modify:




Open the project and choose the following menu:
Project-> setting-> Link
, In
Category
Selected
Output
And then
Reserve
Set the maximum value and
Commit
.



Stack and stack

 

Stack and stack are two different concepts, but we often say "stack" in our habits, which leads to many misunderstandings. Therefore, we need to know the connection and difference between stack and stack.
.

First, we use a diagram to intuitively describe the heap and stack location relationships in memory.

 

 

3.13 stack size and heap size

In Symbian OS, the default stack size of a program is 8 KB by default. However, you can use the keyword epocstacksize in the project's MMP file to adjust its size. For example:

 
 
  1. EPOCSTACKSIZE 0x5000 

The preceding statement changes the stack size of the executable file to 20 KB (0x5000 in hexadecimal format or 20 480 in decimal format ). The maximum stack size is 80 KB.

By default, the minimum heap size is 4 kb, and the maximum heap size is 1 MB. Both can be adjusted. The macro epocheapsize is also used in the project's MMP file. For example:

 
 
  1. EPOCHEAPSIZE 0x5000 0x10000 

In the preceding statement, the minimum heap size is changed to 20 KB (0x5000 bytes) by default, and the maximum heap size is changed to 1 MB (0x10000 bytes ). What does this mean?

If the available memory is less than 20 KB, the process cannot be started again.

Processes cannot consume more than 1 MB of heap memory.

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.