Memory allocation methods and policies

Source: Internet
Author: User

In C, memory is divided into five areas: heap, stack, free storage, global/static storage, and constant storage.

Heap: it is the memory blocks allocated by new. The release compiler does not need to worry about them. It is controlled by the application. Generally, a new one corresponds to a Delete. The programmer has not been released, and the operating system will automatically recycle the program after it is completed.

STACK: it is a storage area for variables allocated by the compiler when needed and automatically cleared when not needed. The variables are usually local variables and function parameters.

Free storage zone: it is the memory block allocated by malloc and so on. It is very similar to the heap, but it uses free to end its own life.

Global/static storage zone: Global and static variables are allocated to the same memory, which occupy the same memory zone together.

Constant storage area: this is a special storage area where constants are stored and cannot be modified.

The point is coming !!!!

Stack and stack differences:

void f() { int* p = new int[5]; }

This short statement contains the heap and stack. When we see new, a heap memory is allocated. For P, it allocates a stack memory. Therefore, this statement stores a pointer P pointing to a heap memory in the stack memory. The program will first determine the size of memory allocated in the heap, then call operator new to allocate the memory, then return the first address of the memory, and put it into the stack. Its assembly code is as follows:

00401028 push 14h0040102A call operator new(00401060)0040102F add esp,400401032 mov dword ptr[ebp-8],eax00401035 mov eax,dword ptr[ebp-8]00401038 mov dword ptr[ebp-4],eax

Here, the Delete P operation is not delete p, but delete [] P to tell the compiler to delete an array, the compiler will release the memory according to the corresponding information.

Top priority !!!

The main differences between stack and stack are as follows:

(1) different management methods

The stack is automatically managed by the compiler. For the stack, the release is controlled by the programmer, which is prone to memory leakage.

(2) space size

Generally, in a 32-bit system, the heap memory can reach 4 GB. From this perspective, there is almost no limit on the heap memory. But for stacks, there is usually a certain amount of space.

(3) Can different fragments be produced?

For the heap, frequent New/delete operations will inevitably cause memory space not to be connected, resulting in a large number of fragments, reducing program efficiency. For stacks, there is no such problem, because stacks come out first and they correspond one by one, so that there is never a memory block to jump out of the middle of the stack.

(4) growth direction

For the heap, the growth direction is upward, that is, the direction for increasing the memory address; For the stack, the growth direction is downward, that is, the direction for decreasing the memory address.

(5) 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 dynamic allocation of stacks is different from that of stacks. The dynamic allocation of stacks is released by the compiler without manual implementation.

(6) Allocation Efficiency

The stack is the data structure provided by the machine system. The computer will provide support for the stack at the underlying layer: assign a special register to store the stack address, and all the inbound and outbound stacks have special instructions for execution, this determines the high efficiency of the stack. The heap mechanism is very complex. For example, in order to allocate a piece of memory, the library function searches for available enough space in the heap memory according to a certain algorithm, if there is not enough space, 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 and then return. Obviously, the heap efficiency is much lower than the stack efficiency.

In short: Both heap and stack should prevent the emergence of out-of-boundary phenomena, because the out-of-boundary result is either a program crash, or a program's heap or stack structure is destroyed to generate unexpected results.



Memory allocation methods and policies

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.