Stacking Zone Global Zone

Source: Internet
Author: User

Memory allocation stacks in iOS programs heap area global zone

In a computer system, the data that runs the application is stored in memory, different types of data, and the memory areas are saved differently.
One, Memory partition

  1. The stack is automatically allocated and freed by the compiler, storing the function's parameter values, local variables, and so on. The stack is the system data structure, and the corresponding thread/process is unique.
    The advantages are fast and efficient, limited in the case of shortcomings, and inflexible data. [Advanced post-out]

    The stack space is divided into two kinds: static allocation and dynamic allocation.

     静态分配是编译器完成的,比如自动变量(auto)的分配。 动态分配由alloca函数完成。 栈的动态分配无需释放(是自动的),也就没有释放函数。 为可移植的程序起见,栈的动态分配操作是不被鼓励的!
  2. The heap is allocated and freed by programmers, and if the programmer does not release it, it may be recycled by the operating system, such as Alloc in iOS, which is stored in the heap.
    The advantages are flexible and convenient, the data adapt to a wide range, but the efficiency has a certain reduction. [Order Free]

     堆是函数库内部数据结构,不一定唯一。 不同堆分配的内存无法互相操作。 堆空间的分配总是动态的

    Although all data spaces are released to the system at the end of the program, the exact application of memory and the release of memory matching are essential elements of a good program.

  3. The storage of global (static) and static variables is put together, the initialized global variables and static variables are stored in an area, uninitialized global variables and static variables are in another adjacent area, and the system is released after the program finishes.

    注意:全局区又可分为未初始化全局区:     .bss段和初始化全局区:data段。     举例:int a;未初始化的。int a = 10;已初始化的。

    Example code:

      int a = 10;  Global initialization zone char *p; Global uninitialized zone main{int b; stack char s[] =  "AB C "Stack char *p1; stack char *p2 = " 123456 "; Span class= "Hljs-number" >123456\\\\0 in the constant area, p2 on the stack. static int C =0; global (static) initialization zone W1 = ( Span class= "Hljs-keyword" >char *) malloc (10); w2 = (char *) malloc (20); 10 and  20 bytes of area are in the heap area. } 
  4. The literal constant area holds the constant string, which is released by the system after the program ends
  5. Binary code of the program code area store function

Second, the system response after the application

    1. Stack: Store each function in the execution of the operating system will ask for resources, the stack is the function of the memory, the stack is responsible for the allocation of variables and release, memory with the function of the allocation, with the function of the end of the release, the system automatically completed.

      Note: 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 report the exception prompt stack overflow.

    2. Heap:
      1. You should first know that the operating system has a linked list that records the free memory address.
      2. When the system receives the application of the program, it iterates through the list, finds the heap node where the first space is larger than the requested space, and then removes the node from the list of idle nodes and assigns the space of that node to the program.
      3. Because the size of the found heap node is not necessarily exactly equal to the size of the request, the system will automatically re-put the extra part in the idle list

Third, the size of the application limit

    1. Stack: A stack is a data structure that extends to a low address and is a contiguous area of memory. Is the top of the stack of the address and the maximum capacity of the system is predetermined, the size of the stack is 2M (also some say 1M, in short, is a compile-time determination of the constant), if the request for more space than the stack of space, will prompt overflow. Therefore, the space available from the stack is small.

    2. Heap: A heap is a data structure that extends to a high address, and is a discontinuous area of memory. This is because the system is stored with a linked list of free memory address, is naturally discontinuous, and the chain of the list of traversal direction is from the low address to high address. The size of the heap is limited by the valid virtual memory in the computer system. Thus, the space of the heap is more flexible and relatively large.


      memory allocation. png

Stack: Automatically allocated by the system, faster, no memory fragmentation
Heap: Is the memory allocated by alloc, slower, and prone to memory fragmentation, but the most convenient to use

To make a metaphor:

Use the stack like we go to a restaurant to eat, just order (send application), pay, and eat (use), eat enough to go, do not bother to cut vegetables, wash vegetables and other preparation work and washing dishes, brush pots and other finishing work, his advantage is fast, but the freedom is small.

The use of the heap is like a DIY dish that you like to eat, more trouble, but more in line with their own tastes, and great freedom.

Stacking Zone Global Zone

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.