zoukankan      html  css  js  c++  java
  • Memory : Stack vs Heap

    Stack

    • very fast access
    • don't have to explicitly de-allocate variables
    • space is managed efficiently by CPU, memory will not become fragmented
    • local variables only
    • limit on stack size (OS-dependent)
    • variables cannot be resized

    Heap

    • variables can be accessed globally
    • no limit on memory size
    • (relatively) slower access
    • no guaranteed efficient use of space, memory may become fragmented over time as blocks of memory are allocated, then freed
    • you must manage memory (you're in charge of allocating and freeing variables)
    • variables can be resized using realloc()

    When to use the Heap?

    When should you use the heap, and when should you use the stack? If you need to allocate a large block of memory (e.g. a large array, or a big struct), and you need to keep that variable around a long time (like a global), then you should allocate it on the heap. If you are dealing with relatively small variables that only need to persist as long as the function using them is alive, then you should use the stack, it's easier and faster. If you need variables like arrays and structs that can change size dynamically (e.g. arrays that can grow or shrink as needed) then you will likely need to allocate them on the heap, and use dynamic memory allocation functions like malloc()calloc()realloc() and free() to manage that memory "by hand". We will talk about dynamically allocated data structures after we talk about pointers.

    https://gribblelab.org/CBootCamp/7_Memory_Stack_vs_Heap.html

  • 相关阅读:
    js常见函数使用
    js数组与函数
    移动端响应式布局
    移动开发之rem布局
    移动flex布局
    移动流式布局
    [剑指offer] 矩阵覆盖
    [剑指offer] 变态跳台阶
    [剑指offer] 跳台阶
    [剑指offer] 斐波那契数列
  • 原文地址:https://www.cnblogs.com/feng9exe/p/12403013.html
Copyright © 2011-2022 走看看