Recall & Review
beginner
What is a memory pool (fixed-size block allocator)?
A memory pool is a way to manage memory by dividing a big chunk into many small fixed-size blocks. It helps quickly allocate and free memory without using the general heap.
Click to reveal answer
beginner
Why use fixed-size blocks in a memory pool?
Fixed-size blocks make allocation and deallocation very fast and simple because each block is the same size. This avoids fragmentation and complex bookkeeping.
Click to reveal answer
intermediate
How does a free list work in a memory pool?
A free list is a linked list of free blocks. When a block is freed, it is added back to the free list. When allocating, a block is taken from the free list.
Click to reveal answer
intermediate
What happens if the memory pool runs out of free blocks?
If no free blocks are left, allocation fails or returns NULL. The program must handle this case to avoid crashes or memory errors.
Click to reveal answer
intermediate
Show a simple C code snippet to initialize a memory pool with fixed-size blocks.
#define BLOCK_SIZE 32
#define BLOCK_COUNT 10
char pool[BLOCK_SIZE * BLOCK_COUNT];
void* free_list = NULL;
void init_pool() {
for (int i = 0; i < BLOCK_COUNT; i++) {
void* block = &pool[i * BLOCK_SIZE];
*(void**)block = free_list;
free_list = block;
}
}
Click to reveal answer
What is the main advantage of using a fixed-size block memory pool?
✗ Incorrect
Fixed-size blocks allow very fast allocation and freeing because the size is constant and bookkeeping is simple.
In a memory pool, what data structure usually tracks free blocks?
✗ Incorrect
A free list is a linked list that keeps track of all free blocks for quick allocation.
What should a program do if the memory pool has no free blocks left?
✗ Incorrect
The program must handle allocation failure by returning NULL or an error to avoid crashes.
Which of these is NOT a benefit of fixed-size block allocators?
✗ Incorrect
Fixed-size block allocators do NOT support variable block sizes; all blocks are the same size.
How is a block added back to the free list when freed?
✗ Incorrect
The freed block is linked to the head of the free list for quick reuse.
Explain how a fixed-size block memory pool manages allocation and deallocation.
Think about how blocks are taken from and returned to the free list.
You got /4 concepts.
Describe what happens when the memory pool runs out of free blocks and how a program should handle it.
Consider what the allocator returns when no blocks are free.
You got /4 concepts.