Skip to content · ⁨Bỏ qua nội dung⁩

Dynamic memory: malloc, free, and realloc · ⁨Dung lượng động: malloc, free, và 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.

Tiếng Việt

Bộ nhớ Stack vs Bộ nhớ Heap

  • Các biến cục bộ thông thường tồn tại trên stack. Chúng xuất hiện khi hàm bắt đầu và biến mất khi hàm kết thúc.
  • Đôi khi bạn cần bộ nhớ vượt thời gian sống của hàm, hoặc kích thước của nó chỉ biết khi chạy chương trình.
  • Bộ nhớ đó lấy từ heap. Bạn yêu cầu nó, và sau đó bạn trả lại nó.

malloc takes a block of heap memory and returns a pointer; free gives the block back

Handout · ⁨Tài liệu⁩

Log in or create account · ⁨Đăng nhập hoặc tạo tài khoản⁩

IGCSE, A-Level & AP