Skip to content

Dynamic memory: malloc, free, and realloc · ⁨动态内存:malloc、free 与 realloc⁩

English

Stack memory vs heap memory

  • Normal local variables live on the stack. They appear when a function starts and vanish when it returns.
  • Sometimes you need memory that outlives the function, or whose size you only know at run time.
  • That memory comes from the heap. You ask for it, and later you give it back.

中文

栈内存与堆内存

  • 普通的局部变量住在栈上。它们在函数开始时出现,在函数返回时消失。
  • 有时你需要的内存要活得比函数久,或者它的大小只有在运行时才知道。
  • 这种内存来自堆。你向它要,之后再还回去。

malloc 取一块堆内存并返回指针;free 把这块内存还回去

Handout

Log in or create account · ⁨登录或创建账户⁩

IGCSE, A-Level · ⁨IGCSE、A-Level⁩ & AP · ⁨与 AP⁩