[Zz] stack and stack differences

Source: Internet
Author: User
Stack and stack differences

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. initialized global variables and static variables
State variables are in one region, and uninitialized global variables and uninitialized static variables are in another adjacent region.
-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, and the memory is allocated using the malloc, calloc, realloc, and other functions.
The configuration is on the stack. In vitro, all functions define a global volume.
Are stored in the global zone (static zone). Static variables defined in all functions in vitro are valid in this file,
It cannot be used in other files. The static expression defined in the function body is valid only in the function body. In addition,
Strings such as "adgfdf" in the function are stored in the constant area.
For example:
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 In the constant area, P3 is on 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 In the constant area, the compiler may point it to "12345"
6 "optimized to one piece.
}
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 insufficient, a large number of arrays and
The hierarchy of recursive functions is too deep. One thing you must know is that when a function is returned after calling it, it will release all
Stack space. 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 when C allocates dynamic memory
Is to find matching 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 are two basic concepts that C/C ++ programming will inevitably encounter. First, these two concepts

They can all be found in the data 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. Research on the underlying machine code can reveal

Stack is the data structure provided by the machine system, while stack 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 embodiment

There are dedicated registers pointing to the address of the stack, and dedicated machine commands to complete the operations of data into and out of the stack.

This mechanism is characterized by high efficiency and limited data support. Generally, it is directly supported by integers, pointers, floating point numbers, and other systems.

Does not directly support other data structures. Because of the characteristics of the stack, the use of the stack in the program

Is very frequent. The call to the subroutine is done directly using the stack. In the call command of the machine

The return address is pushed to the stack and then redirected to the subprogram address. The RET command in the subprogram is implicitly from the stack.

The return address and jump operation are displayed. The automatic variables in C/C ++ are examples of using stacks directly.

When the function returns, the automatic variables of the function automatically become invalid (because the ).

 

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

When using 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, the system calls are used to dynamically increase the memory of the program data segment.

Size. The newly allocated space is first organized into the internal heap and then returned to the caller in an appropriate form.

. When the program releases the allocated memory space, this memory space is returned to the internal Heap Structure and may be properly

Processing (such as merging with other idle spaces into larger idle space) is more suitable for the next memory allocation application. This

A complex allocation mechanism is actually equivalent to a buffer pool (cache) allocated by the memory.

Reason:

1. system calls may not support memory allocation of any size. Some system calls only support fixed sizes and

Multiple Memory requests (allocated 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 and features fast and efficient. Its disadvantage is its limitation and data is not flexible.

Stack is a function provided by the function library, featuring flexibility and convenience, and extensive data adaptation, but the efficiency is reduced.

. The stack is the system data structure, which is unique for processes/Threads. 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 is divided into static allocation and dynamic allocation. Static allocation is editing

The interpreter completes, such as automatic variable (auto) allocation. Dynamic Allocation is completed by the alloca function. Dynamic stack allocation

You do not need to release (automatically) the function. For portable programs, the dynamic stack allocation operation 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.

One piece of thinking
The growth direction of heap and stack is exactly the opposite,
| -------------- | Low address
| Heap |
| -------------- |
|
| I |
|
| ^ |
| Stack | high address
-----------------
Therefore, the heap and stack in the computer are often used together.

In general, it is not necessary to create a node dynamically. It is annoying to use new items as local variables.
Delete.

Reason
1. The stack shard ratio is fast. You only need one command to allocate all the local variables.
2. The stack will not contain memory fragments
3. Stack Object Management

Of course, this is also required in some situations, such
1. large objects
2. The object must be constructed or analyzed at a specific time.
3. Classes only allow dynamic creation of objects. For example, most classes in VCL

Of course, you cannot avoid using heap objects either.

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.