Concept of heap and stack

Source: Internet
Author: User

Store data in memory
In computer systems, the data of running applications is stored in the memory.
Different types of data are stored in different memory regions, including:
1. STACK: (stack) is automatically allocated and released by the compiler. It generally stores function parameter values and local variables.
2. Heap: (HEAP) allocated and released by the program ape. If the program won't be released, it may be recycled by the operating system when the program ends.
3. Register area: used to save the top pointer and command pointer of the stack.
4. global (static): the storage of global and static variables is put together, and the initialized global and static variables are stored in one area, uninitialized global variables and static variables are in another adjacent area, and are released by the system after the program ends.
5. Text Constant Area: stores constant strings, which are released by the system after the program ends.
6. program code area: stores the binary code of the function.

Data in the stack
After the application starts, the operating system will open up memory space for the application in the stack area to store local variables and function parameters.
The size of the IOS main thread stack is 1 m, and the size of the Mac main thread stack is 8 m.
The compiler is responsible for allocating and releasing variables in the stack.
The data in the stack area is managed in the form of a "stack", and the first is the back-to-end (fiber ).
The variables in the access stack area are highly efficient and there will be no memory fragments.
The variable name (without *) in the stack area is equivalent to the pointer alias pointing to the data in the stack area. The variable name can simplify the work of programmers.
The stack address is from high address --> low address.

Heap data
Due to the limited space in the stack area, all objects in IOS applications are built in the stack.
The heap area includes system memory and virtual memory (hard disk memory), which are shared by all running applications.
The operating system uses a linked list to maintain all allocated memory records.
Because the heap area is shared by all applications, the operating system records the allocated memory area anonymously (only the memory address and size are recorded, but not the specific type.
To access data in the heap area, you must use a pointer. the pointer type determines how to access the data in the heap.
When a memory area is no longer used, the program needs to notify the operating system to recycle the memory in this area, so that the memory area can be used again by other programs. Otherwise, this region will never be allocated again. This is the legendary "memory leak ".
If a region has been released and you are still trying to access it, the system will prompt "bad memory access", which is the legendary "Wild pointer access ".
Compared with the stack area, the efficiency in the stack area is much lower and memory fragments are prone to occur.
Compared with the stack area, the access method in the stack area is more flexible, and the memory occupied by objects can be larger.

Concept of heap and 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.