C Language Stack Difference __c language

Source: Internet
Author: User

The difference between heap and stack in C language I. Preface:

C Language Program after compiling the connection to form a compilation, the binary image file formed by the connection is made up of stack, heap, data segment (composed of three parts: read-only data segment, initialized read-write data section, uninitialized data segment that is BBS) and code snippet, as shown in the following figure:


1. Stack area (stack): The compiler automatically assigns the release, storing the function parameter values, local variable equivalence. The operation is similar to the stack in the data structure.

2. Heap Area (heap): typically assigned by programmers to release, if the programmer does not release, it may cause memory leaks. Heap and data structure in the stack is not the same as the class is linked to the list.

3. Program code area: the binary code that holds the function body.

4. Data segment: Consists of three parts:

1> read-only data segment:

Read-only data segments are data that are not changed by the program, and are used in the same way as look-up-table operations, and are only required to be placed in read-only memory because these variables do not need to be changed. Generally, const-decorated variables and literal constants used in programs are typically stored in read-only data segments.

2> initialized Read and write data segments:

Initialized data is a variable that is declared in a program and has an initial value that takes up space in the memory, which needs to be in a read-write memory area when the program executes, and has an initial value for reading and writing when the program is running. In a program, typically a global variable that has already been initialized, a static local variable that has already been initialized (static-decorated, initialized variable)

3> uninitialized Segment (BSS):

Uninitialized data is a variable declared in a program but not initialized, and these variables do not need to occupy storage space before the program runs. Similar to read-write data segments, it also belongs to a static data area. However, the data in this section is not initialized. Uninitialized data segments are only generated during the initialization phase of the operation, so their size does not affect the size of the destination file. In a program, it is generally a global variable that is not initialized and a static local variable that is not initialized. two. Heap and Stack differences

1. Application method

(1) stack (SATCK): Automatically allocated by the system. For example, declare a local variable int b in a function, and the system automatically opens up space for B in the stack.

(2) heap (heap): Require the programmer to apply (call Malloc,realloc,calloc) and indicate the size and release by the programmer. Easy to produce memory leak.

Eg:char p;

p = (char *) malloc (sizeof (char));

However, p itself is in the stack.

2. Application Size Limit

(1) Stack: In Windows under the stack is to the bottom address extension of the data structure, is a contiguous memory area (its growth direction and the direction of memory growth is opposite). The size of the stack is fixed. If the requested space exceeds the remaining space on the stack, the overflow is prompted.

(2) Heap: The heap is a high address extension of the data structure (its growth direction and memory growth direction is the same), is a discontinuous memory area. This is because the system uses a linked list to store the free memory address, the nature is discontinuous, and the chain list traversal direction is from the bottom address to the high address. The size of the heap is limited by the virtual memory available in the computer system.

3. System response:

(1) Stack: As long as the stack space is larger than the application space, the system will provide memory for the program, otherwise it will be reported abnormal tip stack overflow.

(2) Heap: First you should know that the operating system has a record of the free memory address of the linked list, however, when the system receives the application, it traverses the list, looks for the first heap node that is larger than the requested space, then deletes the node from the free list and assigns the node space to the program, and for most systems, The size of this assignment is recorded at the first address in this memory space, so that the free statement in the code can properly release the memory space. In addition, the size of the found heap node is not necessarily equal to the size of the application, the system will automatically put the extra part back into the free list.

Description: For the heap, for the heap, frequent new/delete is bound to cause the memory space discontinuity, resulting in a lot of debris, so that the program efficiency is reduced. For the stack, there is no such problem,

4. Application efficiency

(1) The stack is automatically distributed by the system, and the speed is fast. But programmers can't control it.

(2) The heap is allocated by the malloc memory, the general speed is relatively slow, and easy to produce debris, but the most convenient to use.

5. Storage content in heaps and stacks

(1) Stack: In the function call, the first into the stack of the main function after the address of the next statement, then the function of the parameters, the parameters are from the right to the left into the stack, and then the function of the local variables. Note: Static variables are not in the stack.

When the function call is finished, the local variable first goes out of the stack, then the argument, and the last stack pointer points to the address that was first saved, the next instruction in the main function, where the program continues to execute.

(2) Heap: Usually in the head of the heap with a byte to store the size of the heap.

6. Access efficiency

(1) Heap: char *s1= "Hellow Tigerjibo"; is defined in the compilation

(2) Stack: char s1[]= "Hellow Tigerjibo"; is assigned at run time, with an array faster than the pointer, the pointer in the underlying assembly needs to use the edx registers to relay, and the array read on the stack.

Add:

Stack is the machine system provides the data structure, the computer will support the stack at the bottom: the allocation of special registers to store the stack address, pressure stack out of the stack have specific instructions to execute, which determines the stack of high efficiency. The heap is provided by the C + + function library, and its mechanism is very complex, for example, in order to allocate a piece of memory, the library function will search the heap memory for the available space of sufficient size in a certain algorithm (the specific algorithm can refer to the data structure/operating system), if there is not enough space (possibly due to too much memory fragmentation), 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 get enough memory and then return. Obviously, the heap is much less efficient than the stack.

7. Distribution Mode:

(1) The heap is dynamically allocated and there is no statically allocated heap.

(2) There are two ways to distribute the stack: static allocation and dynamic allocation. Static allocations are done by the compiler, such as the allocation of local variables. Dynamic allocations are allocated by the ALLOCA function, but the dynamic allocation and heap of the stack are different. Its dynamic allocation is released by the compiler without manual implementation.

Related Article

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.