Differences Between Stack and stack in memory

Source: Internet
Author: User
1. Memory Allocation:

Heap: generally causedProgramMembers are assigned to release the program. If the programmer does not release the program, it may be recycled by the OS at the end of the program. Note that it is different from the heap in the data structure. The allocation method is similar to the linked list. The keywords that may be used are new, malloc, delete, and free.

STACK: the compiler automatically allocates and releases the stack, stores the function parameter values, and values of local variables. The operation method is similar to the stack in the data structure.

2. Application Methods:

Heap: the programmer must apply and specify the size. In C, the malloc function is like p1 = (char *) malloc (10). In C ++, the new operator is used. But note that P1 and P2 are in the stack. Because they can still be considered as local variables.

STACK: automatically allocated 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.

3. system response:

Heap: the operating system has a linked list that records the idle memory address. When the system receives a program application, it traverses the linked list to find the heap node with the first space greater than the requested space, delete the node from the idle node linked list and allocate the space of the node to the program. In addition, for most systems, the size of the allocation will be recorded at the first address in the memory space.CodeTo release the memory space correctly. In addition, because the size of the heap node is not necessarily equal to the applied size, the system automatically places the excess part in the idle linked list.

STACK: as long as the remaining space of the stack exceeds the applied space, the system will provide the program with memory. Otherwise, an exception will be reported, prompting stack overflow.

4. size limit:

Heap: it refers to the data structure extended to the high address and is a non-sequential memory area. This is because the system uses the linked list to store the idle memory address, which is naturally discontinuous, And the traversal direction of the linked list is from the low address to the high address. The heap size is limited by the valid virtual memory in the computer system. It can be seen that the space obtained by the heap is flexible and large.

STACK: in windows, a stack is a data structure extended to a low address and a continuous memory area. This sentence means that the top stack address and the maximum stack capacity are pre-defined by the system. In windows, the stack size is fixed (a constant determined during compilation). If the requested space exceeds the remaining space of the stack, overflow is displayed. Therefore, the space available from the stack is small.

5. Efficiency:

Heap: Memory allocated by new. It is generally slow and prone to memory fragments. However, it is most convenient to use. In addition, in windows, the best way is to use virtualalloc to allocate memory, which is not in the heap, nor in the stack. It is to reserve a fast memory directly in the address space of the process, although it is the most inconvenient to use. However, it is fast and flexible.

STACK: the stack is automatically allocated by the system, and the speed is fast. But programmers cannot control it.

6. content storage:

Heap: Generally, the heap size is stored in one byte in the heap header. The specific content in the heap is arranged by the programmer.

STACK: When a function is called, the first entry to the stack is the address of the next instruction in the main function (the next executable statement in the function call statement) and then the parameters of the function, in most C compilers, parameters are written from right to left into the stack, followed by local variables in the function. Note: static variables are not included in the stack. When the function call ends, the local variable first goes out of the stack, then the parameter, and the top pointer of the stack points to the address of the initial storage, that is, the next instruction in the main function, where the program continues to run.

7. access efficiency:

Heap: char * S1 = "Hellow word"; determined during compilation;

STACK: Char S1 [] = "Hellow word"; it is assigned a value at runtime. Using arrays is faster than using pointers, because pointers need to be transferred using the edX register in the underlying assembly, and arrays can be directly read on the stack.

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.