Relationship and difference between stack and stack

Source: Internet
Author: User

I. In C, there are several storage areas
1. Stack-automatically allocated and released by the compiler
2. Heap-generally 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.
3. in the global zone (static zone), global variables and static variables are stored in one partition, and initialized global variables and static variables are stored in one partition, uninitialized global variables and 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 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.

2. In C ++, the memory is divided into five areas: heap, stack, free storage, global/static storage, and constant storage.
1. Stack,
It is the storage area of variables that are automatically allocated by the compiler when necessary and clear when not needed. The variables are usually local variables and function parameters.
2. heap,It is the memory blocks allocated by new. Their release compiler is not controlled and controlled by our application. Generally, a new one corresponds to a Delete. If the programmer does not release the program, the operating system will automatically recycle it after the program is completed.
3. free storage zone,It is the memory blocks allocated by malloc and so on. It is very similar to the heap, but it uses free to end its own life.
4. Global/static storage ZoneGlobal variables and static variables are allocated to the same memory. In the previous C language, global variables were divided into initialized and uninitialized, in C ++, there is no such distinction. They share the same memory zone.
5. Constant storage area,This is a special storage area where constants are stored and cannot be modified (you can also modify them through improper means)

Iii. Relationship and difference between stack and stack
Specifically, modern computers (serial execution mechanisms) directly support the stack data structure at the bottom of the Code. This is reflected in the fact that there are dedicated registers pointing to the address of the stack, and dedicated machine commands to complete the operations of data in and out of the stack. This mechanism is characterized by high efficiency and limited data types supported by systems such as integers, pointers, and floating point numbers. It does not directly support other data structures. Due to the characteristics of stack, the use of stack is very frequent in the program. The call to the subroutine is done directly using the stack. The call command of the machine implicitly pushes the return address into the stack, and then jumps to the subprogram address. The RET command in the subprogram implicitly pops up the return address and jumps from the stack. The automatic variables in C/C ++ are examples of using stacks directly, which is why when the function returns, the function'sAutomatic Variable auto-Failure.
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, the system calls are used to dynamically increase the memory size of the program data segment. 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 processed properly (for example, merged into a larger idle space with other idle space ), it is more suitable for the next memory allocation application. This complex allocation mechanism is actually equivalentBuffer Pool allocated by memory(Cache), the use of this mechanism has the following reasons:
1. system calls may not support memory allocation of any size. Some system calls only support fixed memory requests and their multiples (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, featuring 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, data is widely adapted, 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, which is 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 completed by the compiler, such as automatic variable allocation. Dynamic Allocation is completed by the alloca function. The stack does not need to be released dynamically (automatically), so there is no release function. For the sake of portable programs, dynamic stack allocation is not encouraged! Heap space allocation is always dynamic. Although all the data space will be released back to the system at the end of the program, the memory/
Releasing memory matching is the basic element of a good program.
1. fragmentation: For the heap,Frequent new/delete operations will inevitably lead to memory disconnections, resulting in a large number of fragments, reducing program efficiency.. For the stack, this problem will not exist, because the stack is an advanced and outgoing queue. They are so one-to-one correspondence that it is impossible to have a memory block popped up from the middle of the stack, before the pop-up, the post-stack content on him has been popped up. For details, see> data structure. We will not discuss it one by one here.
2. growth direction: For the stack, the growth direction is upward, that is, the direction to the memory address increase; For the stack, the growth direction is downward, is to increase towards memory address reduction.
3. allocation method: the heap is dynamically allocated without static allocation. There are two stack allocation methods: static allocation and dynamic allocation. Static allocation is completed by the compiler, such as local variable allocation. Dynamic Allocation is implemented by the alloca function, but the stack dynamic allocation is different from the heap dynamic allocation. Its Dynamic Allocation is released by the compiler without manual implementation.
4. allocation Efficiency: the stack is the data structure provided by the machine system, and the computer will provide support for the stack at the underlying layer: allocate a dedicated register to store the stack address, the output stack of the Pressure Stack has dedicated Command Execution, which determinesHigh stack Efficiency. The heap is provided by the C/C ++ function library, and its mechanism is very complicated. For example, to allocate a piece of memory, library functions search for available space in heap memory based on certain algorithms (for specific algorithms, refer to data structures/operating systems, if there is not enough space (probably because there are too many memory fragments), it is possible to call the system function to increase the memory space of the program data segment, so that there is a chance to allocate enough memory, then return. Obviously, the heap efficiency is much lower than the stack efficiency.

From: http://hi.bccn.net/space-369206-do-blog-id-33198.html

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.