It is generally considered that C is divided into these storage areas
1 stack-automatically assigned and released by a compiler
2 heap-it is generally assigned and released by the programmer. If the programmer does not release it, the program may be recycled by the OS at the end of the program.
3. In the global zone (static zone), the storage of global variables and static variables is put together, and the initialized global variables and static variables are stored in one
The uninitialized global variables and the uninitialized static variables are in another adjacent area.
-Release after the program ends
4. There is also a special place to place constants. -Release after the program ends
The variables defined in the function body are usually defined on the stack. What is obtained by the memory allocation function such as malloc, calloc, and realloc?
On the stack. In vitro, all functions define the global volume. After the static modifier is added, all functions are stored in the global zone no matter where they are (static
The static variables defined by all functions in vitro are valid in this file and cannot be used in other files.
The static value is valid only in the function body. 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. The stack is not enough. Generally, the program allocates a large number of arrays and recursive function layers.
Deep. It is important to know that when a function is returned after calling, it will release all the stack space of the function. Stack is automatically managed by the compiler
Yes, 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 produce memory fragments (a bit similar to disk fragments), because C searches for matching when allocating dynamic memory.
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.
Another reprinted article:
Heap and stack are two basic concepts that C/C ++ programming will inevitably encounter. First, both concepts can be discussed in Data
In the structure book, they are all basic data structures, although the stack is simpler.
In a specific C/C ++ programming framework, these two concepts are not parallel. The Research on the underlying machine code can reveal that the stack is a machine system.
The data structure provided by the system, while the heap is provided by the C/C ++ function library.
Specifically, modern computers (serial execution mechanisms) directly support the stack data structure at the bottom of the Code. This is reflected in the dedicated mailing
The memory points to the address where the stack is located, and there are dedicated machine commands to complete the data import and export operations on the stack.
This mechanism is characterized by high efficiency and limited data types supported by systems such as integers, pointers, and floating point numbers,
Other data structures are not directly supported. Due to the characteristics of stack, the use of stack is very frequent in the program. Subprograms
Stack is used directly. The call command of the machine implicitly pushes the return address into the stack, and then jumps to the subroutine address.
While the RET command in the subroutine implicitly pops up the return address from the stack and jumps to the operation. The automatic variables in C/C ++ are directly used.
In the stack example, this is why the automatic variables of the function become invalid when the function returns.
Unlike the stack, the stack data structure is not supported by the system (whether a machine system or an operating system), but provided by the function library.
The basic malloc/realloc/free function maintains an internal heap data structure. When the program uses these functions to obtain new memory
Space, this function first tries to find available memory space from the internal heap. If there is no available memory space, it tries
Use System calls to dynamically increase the memory size of the program data segment. The newly allocated space is first organized into the internal heap, and then
Return to the caller in the appropriate form. When the program releases the allocated memory space, this memory space will be returned to the internal Heap Structure and may
Suitable for processing (such as merging with other idle space into larger idle space), so as to better suit the next memory allocation application. This complex set
The allocation mechanism is actually equivalent to a buffer pool (cache) allocated by memory. There are several reasons for using this mechanism:
1. system calls may not support memory allocation of any size. Some system calls only support fixed memory sizes and multiples.
(Distribution by PAGE); this will cause a waste for a large number of small memory categories.
2. System Call memory application may be expensive. System calls may involve switching between user and core states.
3. Unmanaged memory allocation can easily cause memory fragmentation when a large amount of complex memory is allocated and released.
Stack and stack comparison
From the above knowledge, we can see that stack is a function provided by the system, featuring fast and efficient. Its disadvantage is its limitation and data is not flexible. Stack is a function library.
The features provided are flexible and convenient, and the data is widely adapted, but the efficiency is reduced. Stack is the system data structure.
The process/thread is unique; the heap is the internal data structure of the function library, not necessarily unique. Memory allocated by different heaps cannot be operated on each other. Stack space
It can be divided into two types: static allocation and dynamic allocation. Static allocation is completed by the compiler, such as automatic variable allocation. Dynamic Allocation
The alloca function is complete. The stack does not need to be released dynamically (automatically), so there is no release function. Stack
Dynamic Allocation is not encouraged! Heap space allocation is always dynamic, although all the data space will be released back at the end of the program
System, but the precise request for memory/release memory matching is the basic element of a good program.