Bird
Raised Fist0
Embedded Cprogramming~20 mins

Memory pool (fixed-size block allocator) in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Memory Pool Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of memory pool initialization
What is the output of this code snippet that initializes a fixed-size block memory pool and prints the number of free blocks?
Embedded C
#include <stdio.h>
#include <stdint.h>

#define BLOCK_SIZE 16
#define POOL_SIZE 64

uint8_t pool[POOL_SIZE * BLOCK_SIZE];
uint8_t free_list[POOL_SIZE];

int free_count = 0;

void mempool_init() {
    for (int i = 0; i < POOL_SIZE; i++) {
        free_list[i] = 1; // mark all blocks free
    }
    free_count = POOL_SIZE;
}

int main() {
    mempool_init();
    printf("Free blocks: %d\n", free_count);
    return 0;
}
AFree blocks: 0
BFree blocks: 64
CFree blocks: 16
DFree blocks: 1
Attempts:
2 left
💡 Hint
Look at how free_count is set during initialization.
❓ Predict Output
intermediate
2:00remaining
Output after allocating two blocks
Given this memory pool code, what is the output after allocating two blocks and printing the free count?
Embedded C
#include <stdio.h>
#include <stdint.h>

#define BLOCK_SIZE 8
#define POOL_SIZE 4

uint8_t pool[POOL_SIZE * BLOCK_SIZE];
uint8_t free_list[POOL_SIZE];

int free_count = 0;

void mempool_init() {
    for (int i = 0; i < POOL_SIZE; i++) {
        free_list[i] = 1;
    }
    free_count = POOL_SIZE;
}

void* mempool_alloc() {
    for (int i = 0; i < POOL_SIZE; i++) {
        if (free_list[i]) {
            free_list[i] = 0;
            free_count--;
            return &pool[i * BLOCK_SIZE];
        }
    }
    return NULL;
}

int main() {
    mempool_init();
    mempool_alloc();
    mempool_alloc();
    printf("Free blocks: %d\n", free_count);
    return 0;
}
AFree blocks: 0
BFree blocks: 4
CFree blocks: 2
DFree blocks: 3
Attempts:
2 left
💡 Hint
Each allocation reduces free_count by one.
🔧 Debug
advanced
3:00remaining
Identify the bug causing double free
This memory pool code allows freeing blocks. What bug causes a double free error when freeing the same block twice?
Embedded C
#include <stdio.h>
#include <stdint.h>

#define BLOCK_SIZE 8
#define POOL_SIZE 2

uint8_t pool[POOL_SIZE * BLOCK_SIZE];
uint8_t free_list[POOL_SIZE];

int free_count = 0;

void mempool_init() {
    for (int i = 0; i < POOL_SIZE; i++) {
        free_list[i] = 1;
    }
    free_count = POOL_SIZE;
}

void* mempool_alloc() {
    for (int i = 0; i < POOL_SIZE; i++) {
        if (free_list[i]) {
            free_list[i] = 0;
            free_count--;
            return &pool[i * BLOCK_SIZE];
        }
    }
    return NULL;
}

void mempool_free(void* ptr) {
    int index = ((uint8_t*)ptr - pool) / BLOCK_SIZE;
    free_list[index] = 1;
    free_count++;
}

int main() {
    mempool_init();
    void* p = mempool_alloc();
    mempool_free(p);
    mempool_free(p); // double free
    printf("Free blocks: %d\n", free_count);
    return 0;
}
ANo check if block is already free before freeing it
Bfree_count is not incremented on free
CIndex calculation is incorrect causing out-of-bounds access
DMemory pool is not initialized before use
Attempts:
2 left
💡 Hint
Think about what happens if you free the same block twice.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in memory pool allocation
Which option contains the syntax error in this memory pool allocation function?
Embedded C
void* mempool_alloc() {
    for (int i = 0; i < POOL_SIZE; i++) {
        if (free_list[i]) {
            free_list[i] = 0;
            free_count--;
            return &pool[i * BLOCK_SIZE];
        }
    }
    return NULL;
}
AUsing &pool[i * BLOCK_SIZE] instead of pool + i * BLOCK_SIZE
BIncorrect return type void* instead of int*
CPOOL_SIZE is not defined
DMissing semicolon after free_list[i] = 0
Attempts:
2 left
💡 Hint
Look carefully at each line inside the for loop.
🚀 Application
expert
3:00remaining
Calculate maximum allocatable blocks after mixed operations
Given a memory pool with 10 blocks, after allocating 4 blocks, freeing 2 of them, and allocating 3 more, how many blocks remain free?
Embedded C
#define POOL_SIZE 10

// Initially all blocks free
// Allocate 4 blocks
// Free 2 blocks
// Allocate 3 blocks

// How many free blocks remain?
A5
B3
C4
D6
Attempts:
2 left
💡 Hint
Track allocations and frees step by step.

Practice

(1/5)
1.

What is the main advantage of using a memory pool with fixed-size blocks in embedded systems?

easy
A. Unlimited memory usage
B. Automatic garbage collection
C. Faster and predictable memory allocation
D. Dynamic resizing of blocks

Solution

  1. Step 1: Understand memory pool purpose

    A memory pool divides memory into fixed-size blocks to allocate memory quickly without fragmentation.
  2. Step 2: Identify advantage in embedded systems

    Embedded systems need fast and predictable memory allocation, which memory pools provide.
  3. Final Answer:

    Faster and predictable memory allocation -> Option C
  4. Quick Check:

    Memory pool advantage = Faster and predictable memory allocation [OK]
Hint: Memory pools speed up allocation by fixed block sizes [OK]
Common Mistakes:
  • Confusing memory pools with dynamic resizing
  • Thinking memory pools provide garbage collection
  • Assuming memory pools allow unlimited memory
2.

Which of the following is the correct way to declare a fixed-size memory pool array for 10 blocks of 32 bytes each in C?

?
easy
A. char pool[10][32];
B. char pool[32][10];
C. char *pool[10];
D. char pool[10*32];

Solution

  1. Step 1: Understand array dimensions

    We want 10 blocks, each block is 32 bytes, so array should be 10 rows and 32 columns.
  2. Step 2: Match declaration syntax

    char pool[10][32]; declares 10 blocks of 32 bytes each correctly.
  3. Final Answer:

    char pool[10][32]; -> Option A
  4. Quick Check:

    Array size = 10 blocks x 32 bytes [OK]
Hint: Rows = blocks, columns = block size in bytes [OK]
Common Mistakes:
  • Swapping dimensions (32 and 10)
  • Using pointer array instead of fixed blocks
  • Declaring a single large array without blocks
3.

Given this simplified memory pool allocator code snippet, what will be the output?

char pool[3][16];
int free_blocks = 3;

char* allocate() {
    if (free_blocks > 0) {
        return pool[--free_blocks];
    }
    return NULL;
}

int main() {
    char* p1 = allocate();
    char* p2 = allocate();
    char* p3 = allocate();
    char* p4 = allocate();
    printf("%p %p %p %p\n", p1, p2, p3, p4);
    return 0;
}
medium
A. Three valid pointers and one NULL pointer
B. All pointers are NULL
C. All pointers point to the same block
D. Compilation error due to missing headers

Solution

  1. Step 1: Trace allocation calls

    Initially free_blocks=3. Each allocate() call decrements free_blocks and returns pool[free_blocks]. So p1=pool[2], p2=pool[1], p3=pool[0], p4=NULL because free_blocks=0.
  2. Step 2: Understand output

    First three pointers are valid addresses, last pointer is NULL.
  3. Final Answer:

    Three valid pointers and one NULL pointer -> Option A
  4. Quick Check:

    3 blocks allocated, 4th returns NULL [OK]
Hint: Allocation decreases free_blocks; NULL when none left [OK]
Common Mistakes:
  • Assuming pointers are all NULL
  • Thinking pointers point to same block
  • Forgetting to decrement free_blocks
4.

Identify the bug in this memory pool free function and select the correct fix.

void free_block(char* block) {
    if (free_blocks < MAX_BLOCKS) {
        pool[free_blocks++] = block;
    }
}
medium
A. No bug; function works correctly
B. Incrementing free_blocks before storing pointer causes overwrite
C. Should check if block is NULL before freeing
D. Store pointer in pool array is wrong; should mark block as free differently

Solution

  1. Step 1: Analyze free_block logic

    The pool array holds actual memory blocks, not pointers. Storing a pointer in pool overwrites data.
  2. Step 2: Correct way to free block

    Freeing should mark the block as available in a separate structure or use a free list, not overwrite pool data.
  3. Final Answer:

    Store pointer in pool array is wrong; should mark block as free differently -> Option D
  4. Quick Check:

    Freeing must not overwrite pool memory [OK]
Hint: Freeing must not overwrite pool data blocks [OK]
Common Mistakes:
  • Overwriting pool memory with pointers
  • Not checking block validity
  • Assuming pool holds pointers, not blocks
5.

You want to implement a memory pool that supports 5 blocks of 64 bytes each. You also want to track which blocks are free using a bitmap (an integer where each bit represents a block's status: 0 = free, 1 = used). Which of the following code snippets correctly initializes the bitmap and allocates a block?

?
hard
A. int bitmap = 0xFF; // all used char pool[5][64]; char* allocate() { for (int i = 0; i < 5; i++) { if ((bitmap & (1 << i)) == 0) { bitmap &= ~(1 << i); return pool[i]; } } return NULL; }
B. int bitmap = 0; // all free char pool[5][64]; char* allocate() { for (int i = 0; i < 5; i++) { if ((bitmap & (1 << i)) == 0) { bitmap |= (1 << i); return pool[i]; } } return NULL; }
C. int bitmap = 0; // all used char pool[5][64]; char* allocate() { for (int i = 0; i < 5; i++) { if ((bitmap & (1 << i)) != 0) { bitmap |= (1 << i); return pool[i]; } } return NULL; }
D. int bitmap = 0xFF; // all free char pool[5][64]; char* allocate() { for (int i = 0; i < 5; i++) { if ((bitmap & (1 << i)) != 0) { bitmap &= ~(1 << i); return pool[i]; } } return NULL; }

Solution

  1. Step 1: Understand bitmap meaning

    Bitmap bit 0 means free, 1 means used. So initializing bitmap=0 means all blocks free.
  2. Step 2: Check allocation logic

    Allocation finds first free bit (0), sets it to 1 (used), and returns that block.
  3. Final Answer:

    Bitmap initialized to 0; allocate sets bit to 1 on allocation -> Option B
  4. Quick Check:

    0 = free, set bit to 1 when used [OK]
Hint: 0 bits free, set bit to 1 when allocated [OK]
Common Mistakes:
  • Initializing bitmap with all bits set (all used)
  • Checking wrong bit condition for free blocks
  • Setting bits incorrectly on allocation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard