본문 바로가기

Dynamic memory: malloc, free, and realloc

English

Stack memory vs heap memory

  • Ordinary non-static local variables have automatic storage duration: their lifetime ends when their block finishes. Implementations usually put them on the stack; C does not require a particular memory layout.
  • 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.

int *p points to a block that malloc took from the heap; free(p) gives it back

교재 자료

로그인 또는 계정 만들기

IGCSE, A-Level & AP