Stack & Stack

Source: Internet
Author: User

There are three memory allocation methods:

[1] allocated from the static storage area. The program has been allocated when it is compiled, and the program exists throughout the entire runtime. For example, global variables and static variables.

[2] Create a stack. When a function is executed, the storage units of local variables in the function can be created on the stack. When the function is executed, these storage units are automatically released. Stack memory allocation computation is built into the processor's instruction set, which is highly efficient, but the memory capacity allocated is limited.

[3] distributed from the stack, also known as dynamic memory allocation. When the program runs, it uses malloc or new to apply for any amount of memory. The programmer is responsible for releasing the memory with free or delete. The lifetime of the dynamic memory is determined by the programmer and flexible to use. However, if space is allocated on the stack, the programmer has the responsibility to recycle it. Otherwise, the running program may experience memory leakage, frequent allocation and release of heap space of different sizes will generate fragments in the heap.

The program memory is allocated to the memory occupied by a C/C ++ compiled program. The memory is divided into the following parts:


1. STACK: the compiler automatically allocates and releases the stack, stores the function parameter values, and values of local variables. The operation method is similar to the stack in the data structure. For example, declare a local variable int X in the function; the system automatically opens up space for X in the stack.
2. Heap: Generally, it is 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. In C, the malloc function:

For example, P1 = (char *) malloc (10 );
Use the new operator in C ++
For example, P2 = (char *) malloc (10 );
Note that P1 and P2 are in the stack, while malloc (10) data is stored in the heap. P1 and P2 only point to the data.
3. Global zone (static Zone) (static)

Global variables and static variables are stored in one partition (Global zone = static ZoneThe initialized global variables and static variables are in one area, and uninitialized global variables and uninitialized static variables are in another adjacent area.

After the program ends, the system is released.
4. Text constant area: the constant string is placed here (global area ). The program is released by the System
5. program code area: stores the binary code of the function body.
Example Program
This is written by a senior. It is very detailed.
// Main. cpp
Int A = 0; global initialization Zone
Char * P1; uninitialized globally
Main ()
{
Int B; stack
Char s [] = "ABC"; // Stack
Char * P2; // Stack
Char * P3 = "123456"; // 123456 \ 0 is in the constant zone, and P3 is in the stack.
Static int C = 0; Global (static) initialization Zone
P1 = (char *) malloc (10 );
P2 = (char *) malloc (20 );
// The allocated 10-byte and 20-byte areas are in the heap area.
Strcpy (P1, "123456"); // 123456 \ 0 is placed in the constant area, and the compiler may optimize it into a place with the "123456" pointed to by P3.
}

Application Method

STACK: automatically assigned by the system. For example, declare a local variable int B in the function; the system automatically opens up space for B in the stack.

Heap: the programmer must apply for the heap and specify the size. In C, the malloc function is used, and in C ++, the new operator is used.

For example, P1 = (char *) malloc (10); P1 = new char [10];

For example, P2 = (char *) malloc (10); P2 = new char [20];

But note that P1 and P2 are in the stack.

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, it will traverse the linked list, 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. In addition, for most systems, the size of the allocation will be recorded at the first address in the memory space, so that the delete statement in the code can correctly release the memory space. In addition, because the size of the heap node is not necessarily equal to the applied size, the system automatically places the excess part in the idle linked list.

Application size limit
STACK: in windows, a stack is a data structure extended to a low address and a continuous memory area. This statement indicates that the stack top address and the maximum stack capacity are pre-defined by the system. In Windows, the stack size is 2 MB (OR 1 MB, in short, it is a constant determined during compilation. If the requested space exceeds the remaining space of the stack, overflow will be prompted. 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.

Comparison of application efficiency:
The stack is automatically allocated by the system, which is faster. But programmers cannot control it.
The heap is the memory allocated by new, which is generally slow and prone to memory fragments. However, it is most convenient to use.
In addition, in windows, the best way is to use virtualalloc to allocate memory. Instead of heap or stack, it is to reserve a fast memory in the address space of the process, although it is the most inconvenient to use. However, it is fast and flexible.

Storage content in heap and stack
STACK: when calling a function, the first entry to the stack is the address of the next instruction in the main function (the next executable statement in the function call statement), and then the parameters of the function, in most C compilers, parameters are written from right to left into the stack, followed by local variables in the function. Note that static variables are not included in the stack.
When the function call ends, the local variable first goes out of the stack, then the parameter, and the top pointer of the stack points to the address of the initial storage, that is, the next instruction in the main function, where the program continues to run.
Heap: Generally, the heap size is stored in one byte in the heap header. The specific content in the heap is arranged by the programmer.

Comparison of access efficiency

Char S1 [] = "aaaaaaaaaaaaaaa"; // in the stack
Char * S2 = "bbbbbbbbbbbbbbbbb ";//Constant Area
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
A = C [1];
MoV Cl, byte PTR [ebp-0Fh]
MoV byte PTR [ebp-4], Cl

A = P [1];
MoV edX, dword ptr [ebp-14h]
MoV Al, byte PTR [edX + 1]
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.3.25 supplement

Concept differences

Http://blog.csdn.net/zzzmmmkkk/article/details/4282204

4. Static variables and common variables

Static variables and common variables are stored in the memory strictly separated to ensure the efficiency of using static variables.

Static variable: it is an application-level variable. Its life cycle is the life cycle of the entire application. All the variables that can be accessed by sessions are released after the program ends.

 

Common variable: it is only valid for the current session. After the session is left, or the page is displayed, it is invalid and the space is automatically released.

5. Static local variables and common local variables

Common local variables are valid in the defined functions. After the function is executed, the space is automatically released and will be re-allocated for the next call. Static local variables are only valid in the defined functions, only the program allocates memory once. After the function returns, the variable does not disappear.

 

6. Static global variables and common global variables

Common global variables are valid in the entire project file; static global variables are valid only in the file that defines them.

 

7. Static and common functions

In C, static functions are called internal functions, and they also become static functions. this file can only be used. the lifecycle of a common function is the end of the project. You can use extern to reference external files or modules.

For static and non-static functions of classes: Different call requirements determine their ease of use. Non-static member functions must be called through objects, so an object must be created first; static member functions can be used without creating objects. Therefore, although member functions unrelated to non-static data members of a class can be defined as non-static functions, it is more convenient to use them if they are defined as static functions.
In addition, if a member function of a class is used as a callback function, it can only be defined as a static member.

 


Global variables and local static variables

Http://topic.csdn.net/t/20050808/13/4195727.html

The static data zone exists, and is allocated during compilation.

The storage location is the same,

Although the scope is different.

However, the scope is a syntax level problem. It does not indicate that the areas in the memory are different.

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.