About C + + using pointer heap and stack difference Analysis _c language

Source: Internet
Author: User

Data is stored in memory in the following ways

1. Stack area--Automatically assigned by the compiler and released, the area generally store the function of the parameter values, local variables and other values,
2. Heap area-typically released by programmers, if the programmer does not release, the program will end up being recycled by the operating system,
3. Register area--to hold the stack pointer and instruction pointer
4. Global go-also static area, global variables and static variables are stored together, the initialization of global and static variables are stored in one, for the initialization of global variables and static variables in the adjacent region, the program after the end of the system released.
5. Literal constant area--the constant string is here and released by the system after the program is finished,
6. Program code area--store the binary code of the function.

function parameters and local variables are stored in the stack, the function ends or the return system releases the memory space they occupy, but the global variable is not released by the system. The global is only released by the system after the program is finished, and because global variables are shared by all class members and functions, it is easy to modify, in order to solve this problem
We will use the heap

the difference between stacks

1. Different ways of applying

Heap is the programmer's own application
And the stack is a program to determine the local variable or function system to automatically allocate

2. The system responds differently

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 prompt stack overflow overflow

Heap-The system receives the requested control in the program, will traverse an operating system to record the memory control address of the linked list, when found a space greater than the requested control of the heap node will be removed from the record memory free address of the list, and the nodes of the memory allocated to the program, The size of the allocation is then recorded at the first address of the area, so that when we use Delete to free up memory, delete can correctly identify and delete all the variables in the memory area, and the memory space we apply for is not necessarily equal to the memory space on the heap node. At this point, the system automatically reclaims the extra part of the memory space on the heap node to the free list.

3, the size of space is different

Stack-in Windows state, the stack is a contiguous area of memory, its size is 2M, some say 1M, in short this value is a compile time to determine the constant, is by the system in advance according to the top of the stack address and the maximum capacity of the stack defined, add your data request memory space over the stack of space, You will be prompted to overflow, so don't expect the stack to store larger data.

Heap-Heap is a discontinuous area of memory, each block area by the linked list of them, the concatenated memory space is called Heap, his size is determined by the virtual memory in the system, so the space is relatively large, and the way to obtain space is more flexible.

4. Different efficiency of implementation

Stacks-stacks are automatically allocated by the system, so the speed is faster, but programmers cannot manipulate them.
Heap-Heap is the memory that the programmer allocates, the general speed is slow, and it is easy to produce memory fragment, but it is convenient to use.

5. Different execution functions

Stack-in the function call, the first stack is called the function of the next line of memory address, followed by the function parameters, if the argument is redundant, then the order is from left to right, the last is the function of the local variables.

Because of the advanced post principle of the stack, at the end of the function is just the opposite, first the local variables first out of the stack, then the parameters, the order is from left to right, when even the variables are out of the stack, the pointer natural first into the stack of that line of memory address, that is called function of the next line of memory address, The program jumps to the next line of the calling function based on the address to automatically execute.

Since the stack is advanced after the principle, so he can never produce memory fragments

Heap--The heap is a large stack of contiguous memory regions, in the system by the linked list will be connected to them, so in use must be arranged by the programmer, his mechanism is very complex, sometimes in order to allocate a piece of appropriate memory, programmers need to follow a certain algorithm in the heap memory to search for the available space enough, If there is no room to meet the conditions, it is necessary to send a portion of the memory space in memory, so as to have the opportunity to allocate large enough memory, and then return the calculated value, obviously the heap is much less efficient than the stack, and prone to fragmentation, but the advantage is that the heap can store quite large data, And some details can be arranged by programmers.

The above is the difference between the stack but in the application of the choice or specific problems specific analysis

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.