iOS data storage type and heap (heap) and stack (stack)

Source: Internet
Author: User

iOS data storage type and heap (heap) and stack (stack)

It is generally thought that in C there are several storage areas:

1 Stacks --automatically allocated for release by the compiler.
2 heap -typically released by programmers, if the programmer does not release, the program may end up being recycled by the OS.
3 Global Zone (static storage)--The storage of global variables and static variables is placed in an area and automatically released after the program exits. The global zone is also divided into global initialization zone and global uninitialized zone. The initialized global variables and static variables are stored in the global initialization area, and uninitialized global variables and uninitialized static variables are stored in another contiguous area.

4 constant Area -the place where the number/character constant is placed, automatically released after the program exits.

The automatic variables defined in the function body are usually on the stack ,

With Malloc,calloc, realloc and other allocated memory functions are allocated on the heap .

After adding the static modifier, it is stored in the Global zone (static store) regardless of where it is located.

Global variables are defined in the body of all functions, stored in the Global zone (static storage), and are available through the extern declaration in all the source files of the C program.

A static global variable defined outside the body of all functions is stored in the Global zone (static store) and is valid only in that file and cannot be declared to another file by extern.

Static variables defined in the function body are stored in the Global zone (static store), which means that the function is valid only within the body, but its life cycle becomes the same as the entire program.

In addition, a string such as "ADGFDF" in the function is stored in a constant area .

Code:
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 area
P1 = (char *) malloc (10); The allocated 10-byte area is in the heap area.
P2 = (char *) malloc (20); The allocated 20-byte area is in the heap area.
strcpy (P1, "123456"); 123456\0 is placed in a constant area, the compiler might optimize it with the "123456" that P3 points to.
}


A function call will have a series of operations on the stack that hold the scene and pass parameters. The size of the stack is limited, the VC default is 2M. Stack is not enough to use the situation is generally allocated in the program a large number of arrays and recursive function hierarchy too deep. It is important to know that when a function call is finished, it frees all the stack space in the function. Stacks are automatically managed by the compiler, and you don't have to worry about them.


The heap is dynamically allocated memory, and you can allocate it with very large memory. However, a memory leak is caused by bad use. and frequently malloc and free generate memory fragmentation (a bit like disk fragmentation), because C allocates dynamic memory when it is looking for matching memory. The use of stacks does not result in fragmentation.


Generally speaking, stacks and stacks are the same, that is, stacks (stack), and heap is the heap. Accessing data on the stack is faster than accessing the data on the heap via pointers. Stacks are first-in, usually from high-address to low-address growth.


Heap and Stack (stack) are the two basic concepts that are inevitably encountered in C/s + + programming.

The stack is the data structure provided by the machine system, and the heap is provided by the C/S function library.
Specifically, the modern computer (serial execution mechanism) directly supports the data structure of the stack at the bottom of the code. This is reflected in, there is a dedicated register to the address of the stack, there is a dedicated machine instruction to complete the data into the stack out of the stack operation.
This mechanism is characterized by high efficiency, limited data support, generally integer, pointer, floating point and other systems directly supported by the data type, does not directly support other data structures. Because of this feature of the stack, the use of stacks is very frequent in the program.

The automatic variable in C + + is an example of using the stack directly, which is why the automatic variable of the function automatically fails when the function returns.

Unlike stacks, the data structure of a heap is not supported by the system (whether it is a machine system or an operating system), but is provided by a library of functions.
The basic Malloc/realloc/free function maintains a set of internal heap data structures. When the program uses these functions to obtain new memory space, the function first tries to find the available memory space from the internal heap, if there is no memory space available, then attempts to use the system to dynamically increase the size of the program data segment memory, the newly allocated space is first organized into the internal heap, It is then returned to the caller in the appropriate form. When the program frees the allocated memory space, this memory space is returned to the internal heap structure and may be appropriately processed (such as merging additional free space into a larger free space) to better fit the next memory allocation request. This complex allocation mechanism is actually equivalent to a memory allocation buffer pool (cache), which has several reasons for using this set of mechanisms:

1. System calls may not support memory allocations of any size. Some system calls only support a fixed size and multiple memory requests (per page allocation), which can be wasteful for a large number of small memory classifications.

2. System calls to request memory can be costly. System calls may involve conversion of the user state and kernel mentality.

3. Memory allocations that are not managed can easily cause memory fragmentation under the allocation release operation of a large amount of complex memory.


Comparison of heaps and stacks

From the above knowledge, the stack is the function provided by the system, the characteristics are fast and efficient, the disadvantage is limited, the data is not flexible, and the stack is the function library provides the function, the characteristic is flexible and convenient, the data adapts widely, but the efficiency has certain reduction.

A stack is a system data structure that is unique to a process/thread, and a heap is a library internal data structure that is not necessarily unique. Memory allocated by different heaps cannot operate with each other. Stack space
Static distribution and dynamic allocation. Static allocations are done by the compiler, such as the automatic variable (auto) assignment. Dynamic allocation is done by the Alloca function. The dynamic allocation of the stack does not have to be released (it is automatic), and there is no release function. For portable programs, the Stack's
Dynamic allocation operations are not encouraged!

The allocation of heap space is always dynamic, although all data space will be released back to the system at the end of the program, but an accurate memory/release memory match is an essential element of a good program.

iOS data storage type and heap (heap) and stack (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.