0
0
Embedded Cprogramming~20 mins

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

Choose your learning style9 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.