What is the difference between stack and heap?

First, heap and stack discussion refers to the memory of the "heap" and "stack area", OC language is a superset of the C language, so to understand memory model of the C language memory management will be of great help. C language memory model is divided into five zones: Zone stack, heap, static area, constant area code area. The contents of each storage area is as follows:

1, the stack area: storing the function parameters, local variables, etc., is automatically allocated and freed by the compiler, typically on the release function is completed, the operation in a manner similar to the data structure of the stack. Stack memory allocation operation built in the CPU instruction set, high efficiency, but the limited amount of memory allocated, such as the size of the stack area iOS is 2M.

2, the heap: through memory block new, malloc, realloc assignment, the compiler will not be responsible for their release task, requiring the release of a program area. Distribution similar to the data structure list. He said in iOS development "memory leaks" say is the memory heap area.

3, the static area: global and static variables (in iOS is a modified static local variable or global global variables) is stored on one of, initialized global and static variables in an area, uninitialized global uninitialized variables and static variables in an area adjacent to another. After the end of the program, released by the system.

4, constant zones: constants stored here can not be modified.

5, Area Code: storing binary code function body.

 

Heap and stack difference:

1, the memory heap space is dynamically allocated, the general object stored, and the need to manually release the memory. Of course, iOS introduced ARC (automatic reference count management technique), the programmer would need to use the object code management memory, before the MRC (manually manage memory), programmers need to manually release the object. Further, ARC is only an intermediate layer technology, although before the ARC mode, the programmer does not need so much trouble as memory management, but to follow the standard operation ARC technique, such as using attribute qualifiers weak, strong, assigen like. Therefore, if the programmer does not rule by the ARC and the rational use of these properties qualifiers, it also can cause a memory leak.

2, the memory stack space is automatically allocated by the system, a general store local variables, such as the address of the object equivalent, programmers do not need to manage this memory, for example, the role of the local variable range of the function (lifetime) is after completion of the transfer function is over. These levels have been defined in the system live, the programmer only needs programming in this constraint is good, this is simply not the right memory management to the programmer, certainly there would be no let a programmer management Say.

 

Speaking in terms of the size of the application:

Stack space is relatively small;

Heap space is relatively large.

 

From the data storage terms:

Stack space typically stores basic data type, address of the object;

General storage heap space object itself, block of copy and so on.

 

Guess you like

Origin www.cnblogs.com/cchHers/p/10010275.html