What is Dynamic memory allocation?

Obtaining memory of the needed size while a program is running, and releasing it when it is no longer used. In C, malloc(size) allocates it and returns the address of its start, and free(address) releases it. calloc returns a zero-initialized region and realloc returns a resized one. On failure, NULL is returned, so the result must be checked. The region lies in the heap and survives function returns until it is released. On small microcontrollers it is often avoided because of fragmentation and allocation times that are hard to predict.

Japanese name: 動的メモリ確保

Dynamic memory allocation appears in 7 practice questions across 4 topics, including C Programming Basics, Linkers, Loaders & Executables, Embedded Systems & Firmware.

Field: C Programming Basics

Try it: questions about Dynamic memory allocation

Memory allocated with C's malloc generally comes from which area of a process?

  1. AThe .text section
  2. BThe heap
  3. CThe .rodata section
Show answer and explanation

Answer: B. The heap

The heap is the area from which a running program obtains memory of the size it needs and returns it with free when done. Its key feature is that sizes and lifetimes are decided at run time. malloc manages free space itself and asks the OS to enlarge the area when it runs short (through the brk or mmap system calls on Linux). Forgetting to call free leaves unused memory behind, which is a memory leak.

After repeated malloc and free, a large block may become impossible to allocate even though total free memory is sufficient. What is this phenomenon called?

  1. AA memory leak
  2. BFragmentation
  3. CA stack overflow
Show answer and explanation

Answer: B. Fragmentation

Repeatedly allocating and freeing blocks of different sizes scatters small free gaps between blocks in use. Even if total free space is large, a big allocation fails without a large contiguous gap. This is called fragmentation. Allocators reduce it by managing blocks by size class and merging adjacent free blocks. It needs special care in embedded devices that run for a long time.

Dynamic memory allocation in the explanations

Questions that mention Dynamic memory allocation (7)

  1. Memory allocated with C's malloc generally comes from which area of a process?
  2. After repeated malloc and free, a large block may become impossible to allocate even though total free memory is sufficient. What is this phenomenon called?
  3. Why does firmware on small microcontrollers often avoid dynamic memory allocation such as malloc?
  4. In C, which of the following correctly pairs the lifetimes of (1) a local variable declared inside a function without static, (2) a region allocated with malloc, and (3) a variable declared outside any function?
  5. Under the first-touch policy common on Linux NUMA systems (a page is placed in the memory of the node where the thread first writing it runs), what should programmers watch out for?
  6. fill and use are functions defined elsewhere. Which of the following is a problem with the function above?
  7. Which of the following is true about the pointer returned by the function make above?

Books on C Programming Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Heap memory Memory leak Variable Stack Pointer Memory fragmentation Microcontroller (MCU) Non-uniform memory access (NUMA) Page fault Array

Back to the glossary