# Include <stdio. h>
# Include <malloc. h>
Void print (char *, INT );
Main ()
{
Char * S1 = "ABCDE ";
Char * S2 = "ABCDE ";
Char S3 [] = "ABCD ";
Long int * S4 [100];
Char * S5 = "ABCDE ";
Int A = 5;
Int B = 6; // A, B on the stack, & A> & B address reverse growth
Printf ("variables address in main function:/n/
S1 = % P S2 = % P S3 = % P S4 = % P S5 = % p a = % p B = % P/n ",/
S1, S2, S3, S4, S5, & A, & B );
Printf ("variables address in process call:/N ");
Print ("ddddddddd", 5); // The parameter goes into the stack from the right to the left, P is advanced, STR is followed by & P> & Str
Printf ("/nmain = % P print = % P/N", main, print );
// Print the address of the main function and sub-function in the code segment. The compiled address is low, and the compiled address is high. Main <print
}
Stack and heap are dynamically allocated when the program is running, and local variables are allocated on the stack. The stack increases in reverse direction and the address decreases; Malloc and other allocated memory space in the heap space. The heap is growing positively and the address is increasing progressively. R, W variables on the stack (then & R> & W), R, W content in the heap (that is, r <W ). |
===================================== The following content is reprinted on the Internet ==================== ==============
In C, it is generally considered to be divided into the following storage areas:
1. Stack -- automatically assigned and released by a compiler
2. Heap-generally assigned and released by the programmer. If the programmer does not release, the program may be recycled by the OS at the end of the program.
3. global zone (static zone)-storage of global and static variables is placed in one area, and initialized global and static variables are located in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. The program ends and is released.
4. There is also a special place to place constants. The program ends and is released. The variables defined in the function body are usually on the stack. The memory allocated by using malloc, calloc, realloc, and other functions is on the stack. All functions define a global volume in vitro. After the static modifier is added, all functions are stored in the global zone (static zone) no matter where they are located ), static variables defined by all functions in vitro are valid in this file and cannot be used in other files. Static variables defined in the function body are valid only in this function. In addition, strings such as "adgfdf" in the function are stored in the constant area.
For example: code:
Int A = 0; // global initialization Zone
Char * P1; // not initialized 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 with "123456" pointed to by P3.
}
In addition, during function calling, a series of operations will be performed on the stack to retain the site and transmit parameters.
The stack space is limited. The default value of VC is 2 MB. When the stack is not enough, it is generally because the program has allocated a large number of arrays and recursive function layers too deep. It is important to know that when a function is returned after calling, it will release all the stack space of the function. The stack is automatically managed by the compiler, so you don't have to worry about it.
Heap dynamically allocates memory, and you can allocate a large amount of memory. However, poor use may cause memory leakage. In addition, frequent malloc and free will generate memory fragments (a bit similar to disk fragments), because C searches for matching memory when allocating dynamic memory. Stack does not produce fragments.
Accessing data on a stack is faster than accessing data on a stack through a pointer.
Generally, stack is the same as stack, that is, stack, and heap. the stack is first imported and then output, and is generally grown from a high address to a low address.
Heap and stack
2.1 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 needs to apply and specify the size. In C, the malloc Function
For example, P1 = (char *) malloc (10 );
Use the new operator in C ++
For example, P2 = (char *) malloc (10 );
But note that P1 and P2 are in the stack.
2.2 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,
The linked list is traversed to find the heap node with the first space greater than the requested space. Then, the node is deleted from the idle node linked list and allocated to the program, 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.
2.3
2.4 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 2.5 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.
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:
# I nclude <...>
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.
The differences between stack and stack are as follows:
The heap and stack of the operating system, as mentioned above, will not be mentioned much.
There is also the heap and stack in the data structure. These are different concepts. Here, the heap actually refers to a Data Structure (meeting the heap nature) of the priority queue. The 1st elements have the highest priority; stack is actually a mathematical or data structure that meets the needs of the advanced and later stages.
Although the stack is called a connection, there is a lot of difference between the stack and the stack.
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. Ii. Theoretical knowledge of heap and stack I. Preparation knowledge-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 in one area, 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. Code area (text)-stores the binary code of the function body.
Ii. Example Program
This is written by a senior. It is very detailed.
// Main. cpp
Int A = 0; // global initialization Zone
Char * P1; // not initialized 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 with "123456" pointed to by P3.
} Void print (char * STR, int P)
{
Char * S1 = "ABCDE"; // ABCDE is in the constant zone, and S1 is in the stack.
Char * S2 = "ABCDE"; // ABCDE in the constant area, S2 in the stack s2-s1 = 6 may be equal to 0, the compiler optimized the same constant, only save one copy in the memory
// And & S1> & S2
Char S3 [] = "abcdeee"; // abcdeee is in the constant area, S3 is on the stack, and the content stored in the array is a copy of abcdeee.
Long int * S4 [100];
Char * S5 = "ABCDE ";
Int A = 5;
Int B = 6;
Int C;
Int D; // a, B, c, d are all on the stack, & A> & B> & C> & D address reverse growth
Char * q = STR ;//
Int M = P ;//
Char * r = (char *) malloc (1 );
Char * w = (char *) malloc (1); // R <W heap is growing
Printf ("S1 = % P S2 = % P S3 = % P S4 = % P S5 = % P/NA = % p B = % P C = % P d = % P /n/
STR = % p q = % p m = % p r = % P w = % P/N ",
S1, S2, S3, S4, S5, & A, & B, & C, & D, & STR, Q, & P, & M, R, W );
}