C language: What is the difference between heap and stack?

First of all, the heap and stack discussed refer to the "heap area" and "stack area" in memory. The OC language is a superset of the C language, so it will be of great help to understand the memory management of the memory model of the C language. The memory model of C language is divided into 5 areas: stack area, heap area, static area, constant area, and code area. The contents stored in each area are as follows:

1. Stack area: store function parameter values, local variables, etc., are automatically allocated and released by the compiler, usually released after the function is executed, and its operation mode is similar to the stack in the data structure. The stack memory allocation operation is built into the instruction set of the CPU, which is very efficient, but the amount of allocated memory is limited. For example, the size of the stack area in iOS is 2M.

2. Heap area: It is the memory block allocated by new, malloc, and realloc. The compiler will not be responsible for their release, and it needs to be released in the program area. The allocation method is similar to the linked list in the data structure. The "memory leak" in iOS development refers to the memory in the heap area.

3. Static area: The storage of global variables and static variables (local variables modified with static or global global variables in iOS) are stored together, the initialized global variables and static variables are in one area, the uninitialized global Variables and uninitialized static variables are in another adjacent area. After the program ends, it will be released by the system.

4. Constant area: Constants are stored here and cannot be modified.

5. Code area: store the binary code of the function body.

堆和栈的区别:

  • 1. The memory of the heap space is dynamically allocated. Objects are generally stored, and the memory needs to be manually released. Of course, after iOS introduced ARC (Automatic Reference Counting Management Technology), programmers no longer need to use code to manage the memory of objects. In the previous MRC (manual memory management), programmers needed to manually release objects. In addition, ARC is only a middle-tier technology. Although in ARC mode, programmers do not need to manage memory as much as before, but they need to follow the standard operations of ARC technology, such as using attribute qualifiers weak, strong, and assigen. Therefore, if the programmer does not follow the ARC rules and use these attribute qualifiers reasonably, it will also cause memory leaks.

  • 2. The memory in the stack space is automatically allocated by the system, and generally stores local variables, such as the address of an object, and does not require programmers to manage this memory. For example, the scope (life cycle) of local variables in a function is It ends after tuning this function. These have been limited at the system level. The programmer only needs to program under this constraint. The management of this memory is not given to the programmer at all, and there is certainly no need for the programmer to manage it. Say.

从申请的大小方面讲:

  • The stack space is relatively small;

  • The heap space is relatively large.

从数据存储方面来说:

  • Basic data types and object addresses are generally stored in the stack space;

  • The heap space generally stores the object itself, copy of the block, and so on.

Guess you like

Origin blog.csdn.net/houxiaoni01/article/details/103582618