The distinction between stacks and heaps

Source: Internet
Author: User

The difference between stacks and heaps

First, Memory Classification of the program

Version One turn to everyone forum

C + + compiled programs occupy a memory category:
1, stack------automatically allocated by the compiler to release, store the function parameter value, local variable value and so on. It works like a stack in a data structure (a LIFO method).

2, heap area (heap)------is generally released by the programmer, if the programmer does not release, the program ends may be recycled by the OS. Note that it is not the same as the heap in the data structure, but the distribution is similar to the linked list.

3, Global, static storage area (global,statics)

The storage of global variables and static variables is placed in a block, initialized global variables and static variables in an area, uninitialized global variables and uninitialized static variables in another contiguous area. -System release after the program is finished
4, the literal constant area-----constant string is put here. Released by the system after the program is finished
5, The program code area-----The binary code that holds the function body.

Program Area

Static Storage Area

Dynamic Storage (Stack)

version Two refer to the C Language Program Design (China Mining Large publishing house)

When the C language program runs, its code and variables are placed in memory and registers, because the number of memory is very small, so memory is the C program important storage area. The memory occupied by the C program can be divided into three parts.

Program Area : The machine instruction that holds the executable program;

static storage : Data that needs to occupy a fixed storage unit, such as global variables;

Dynamic Storage : The stored data is dynamically allocated and released, including formal parameters of functions, automatic variables (preceded by the keyword Auto), and the address returned by the Field protection box when the function is called.

Local variable definitions are available with auto, static, register, extern

Automatic variable int a, B; equivalent to int auto A, b;

Static local variables: Static local variables are used when a function is called multiple times and the value of certain variables is required to be preserved between calls. Its lifetime is the entire program, but it is used only within the function that defines the variable. After exiting the function, it cannot be used even though the variable continues to exist.

Register variables are stored in the registers of the CPU, and are read and written directly from registers because they do not require access to memory when using register variables, so frequently reused variables are stored in registers to speed up the program. However, only automatic local variables and formal parameters can be used as register variables.

Second, example

It was written by a predecessor, very detailed
Main.cpp
int a = 0; Global initialization Zone
Char *p1; Global uninitialized Zone
Main ()
{
int b; Stack
Char s[] = "ABC"; Stack
Char *p2; Stack
Char *p3 = "123456"; 123456/0 in the constant area, p3 on the stack.
static int c = 0; global (static) initialization zone
P1 = (char *) malloc (10);
P2 = (char *) malloc (20);
Areas that are allocated 10 and 20 bytes are in the heap area.
strcpy (P1, "123456"); 123456/0 in the constant area, the compiler might optimize it to a place with the "123456" that P3 points to.
}

third, heap and stack of theoretical knowledge  
2.1 How to apply

The stack is 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: Requires programmers to apply themselves and indicate size, malloc, calloc function in C, and new function in C + +
such as P1 = (char *) malloc (10); P1 = (char *) calloc (10,5);

But note that P1, p2 itself is in the stack.


2.2
Response of the system after application
Stack: As long as the remaining space of the stack is larger than the requested space, the system will provide memory for the program, otherwise it will report the exception prompt stack overflow.
Heap: First of all should know that the operating system has a record of the free memory address of the list, when the system receives the application of the program, it will traverse the list, look for the first space is larger than the requested space of the heap node, and then delete the node from the list of idle nodes, and the node's space allocated to the program.

Also, for most systems, the size of this allocation is recorded at the first address in the memory space, so that the delete (or free) statement in the code can properly release the memory space. Also, because the size of the found heap node does not necessarily equal the size of the request, the system automatically re-places the extra portion into the idle list.

2.3 Application Size Limits
Stack: Under Windows, the stack is the data structure to the low address extension, which is a contiguous area of memory. This sentence means that the top of the stack of the address and the maximum capacity of the stack is the system pre-defined, in Windows, the size of the stack is 2M (also said 1M, in short, is a compile-time determination of the constant), if the request for more space than the stack's remaining space, will prompt overflow. Therefore, the space available from the stack is small.
Heap: A heap is a data structure that extends to a high address, and is a discontinuous area of memory. This is because the system is stored with a linked list of free memory address, is naturally discontinuous, and the chain of the list of traversal direction is from the low address to high address. the size of the heap is limited by the valid virtual memory in the computer system. Thus, the space of the heap is more flexible and relatively large.


2.4 Application Efficiency:
The stack is automatically assigned by the system and is faster. But programmers can't control it.
Heap is the memory allocated by new, the general speed is relatively slow, and prone to memory fragmentation, but the most convenient to use.
In addition, under Windows, the best way is to use VirtualAlloc to allocate memory, he is not in the heap, nor in the stack is directly in the process's address space to keep a fast memory, although the most inconvenient to use. But the speed is fast, also the most flexible.

2.5 Storage contents in stacks and stacks
Stack: In a function call, the first stack is the address of the next instruction in the main function (the next executable statement of the function call statement), and then the parameters of the function, in most C compilers, the arguments are left-to-right and then the local variables in the function. Note that static variables are not in the stack.
When the function call is finished, the local variable is first out of the stack, then the parameter, and the last stack pointer points to the first saved address, which is the next instruction in the main function, and the program continues to run from that point.
Heap: The size of a heap is typically stored in a heap at the head of a pile. The concrete contents of the heap are arranged by programmers.

The distinction between stacks and heaps

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.