What is the difference between heap and stack in iOS?

Source: Internet
Author: User

What is the difference between heap and stack in iOS?

The main differences between stack and stack are as follows:

1. Management Method: For stacks, it is automatically managed by the compiler without manual control. For heaps, the release work is controlled by the programmer and memory leak (memory leakage) is easily generated ).

2. Application size:

Stack is a data structure extended to a low address and a continuous memory area. This statement indicates that the address at the top of the stack and the maximum stack capacity are pre-defined by the system, which is a constant determined at the time of compilation. If the requested space exceeds the remaining space of the stack, overflow will be prompted ). Therefore, the space available from the stack is small.

The heap is a data structure extended to the high address and a non-sequential memory area. This is because the system stores the idle memory address by the linked list, which is naturally discontinuous, And the traversal direction of the linked list is from the low address to the high address. The heap size is limited by the valid virtual memory in the computer system. It can be seen that the space obtained by the heap is flexible and large.

3. Fragmentation issues:

For the heap, frequent new and delegate will inevitably lead to memory space disconnections, resulting in a large number of fragments and reduced program efficiency. For the stack, this problem does not exist, because the stack is an advanced and outgoing queue, they are so one-to-one correspondence that it is impossible to have a memory block popped up from the middle of the stack.

4. allocation method:

There are two stack allocation methods: static allocation and dynamic allocation. Static allocation is done by the compiler, such as local variable allocation. Dynamic Allocation is implemented by the alloca function, but the stack dynamic allocation is different from the heap dynamic allocation. Its Dynamic Allocation is released by the compiler without manual implementation.

The heap is dynamically allocated without static allocation.

5. Allocation Efficiency:

The stack is the data structure provided by the machine system. The computer will provide support for the stack at the underlying layer, allocate a special register to store the stack address, and all the output stacks of the Pressure stack have special command execution, this determines the high efficiency of the stack. The heap is provided by the C/C ++ function library, and its mechanism is very complicated.

Related Article

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.