Bird
Raised Fist0
FreeRTOSprogramming~10 mins

FreeRTOS heap implementations (heap_1 to heap_5) - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select the simplest FreeRTOS heap implementation that does not allow memory to be freed.

FreeRTOS
void *pvPortMalloc( size_t xSize ) {
    static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
    static size_t xNextFreeByte = 0;

    if( ( xNextFreeByte + xSize ) < configTOTAL_HEAP_SIZE ) {
        void *pvReturn = &ucHeap[ xNextFreeByte ];
        xNextFreeByte += [1];
        return pvReturn;
    }
    return NULL;
}
Drag options to blanks, or click blank then click option'
AxSize
BconfigTOTAL_HEAP_SIZE
Csizeof(ucHeap)
DxNextFreeByte
Attempts:
3 left
💡 Hint
Common Mistakes
Using the total heap size instead of the requested size.
Incrementing by the current pointer value instead of the requested size.
2fill in blank
medium

Complete the code to implement a FreeRTOS heap that supports memory freeing by using a linked list of free blocks.

FreeRTOS
typedef struct A_BLOCK_LINK {
    struct A_BLOCK_LINK *pxNextFreeBlock;
    size_t xBlockSize;
} BlockLink_t;

static BlockLink_t xStart, *pxEnd = NULL;

void vPortFree( void *pv ) {
    BlockLink_t *pxLink = ( BlockLink_t * ) pv - 1;
    pxLink->pxNextFreeBlock = xStart.pxNextFreeBlock;
    xStart.pxNextFreeBlock = [1];
}
Drag options to blanks, or click blank then click option'
ApxLink
BpxEnd
Cpv
DxStart
Attempts:
3 left
💡 Hint
Common Mistakes
Linking the free list to the end pointer instead of the freed block.
Using the void pointer directly instead of casting to block link.
3fill in blank
hard

Fix the error in the heap_3 implementation where the allocation function does not correctly align the block size.

FreeRTOS
size_t xWantedSize = xSize + sizeof( BlockLink_t );

/* Align the size to the nearest multiple of [1]. */
xWantedSize = ( xWantedSize + ( [2] - 1 ) ) & ~ ( [3] - 1 );
Drag options to blanks, or click blank then click option'
Asizeof( void * )
Bsizeof( BlockLink_t )
CconfigMINIMAL_STACK_SIZE
DconfigCPU_CLOCK_HZ
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated constants for alignment.
Not aligning the size at all, causing misaligned memory.
4fill in blank
hard

Fill both blanks to complete the heap_4 allocation function that uses a best-fit algorithm to find a free block.

FreeRTOS
BlockLink_t *pxBlock = xStart.pxNextFreeBlock;
BlockLink_t *pxBestBlock = NULL;
size_t xBestSize = ( size_t ) -1;

while( pxBlock != pxEnd ) {
    if( pxBlock->xBlockSize >= [1] ) {
        if( pxBlock->xBlockSize < xBestSize ) {
            pxBestBlock = pxBlock;
            xBestSize = pxBlock->xBlockSize;
        }
    }
    pxBlock = pxBlock->pxNextFreeBlock;
}

if( pxBestBlock != NULL ) {
    /* Allocate from pxBestBlock */
    /* ... */
}
Drag options to blanks, or click blank then click option'
AxWantedSize
BxBlockSize
CxSize
DconfigTOTAL_HEAP_SIZE
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing to the wrong size variable.
Not updating the best block when a smaller suitable block is found.
5fill in blank
hard

Fill all three blanks to complete the heap_5 allocation function that uses a first-fit algorithm and coalesces adjacent free blocks.

FreeRTOS
BlockLink_t *pxPreviousBlock = &xStart;
BlockLink_t *pxBlock = xStart.pxNextFreeBlock;

while( pxBlock != pxEnd ) {
    if( pxBlock->xBlockSize >= [1] ) {
        /* Remove block from free list */
        pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;

        /* Split block if large enough */
        if( pxBlock->xBlockSize - [2] > [3] ) {
            /* Create new free block with remaining size */
            /* ... */
        }
        return ( void * )( pxBlock + 1 );
    }
    pxPreviousBlock = pxBlock;
    pxBlock = pxBlock->pxNextFreeBlock;
}
Drag options to blanks, or click blank then click option'
AxWantedSize
Csizeof( BlockLink_t )
DconfigMINIMAL_STACK_SIZE
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect sizes for splitting condition.
Not updating the free list pointers correctly.

Practice

(1/5)
1. Which FreeRTOS heap implementation does NOT support freeing allocated memory?
easy
A. heap_1
B. heap_2
C. heap_3
D. heap_4

Solution

  1. Step 1: Understand heap_1 behavior

    heap_1 only allows allocation but does not provide a function to free memory once allocated.
  2. Step 2: Compare with other heaps

    heap_2, heap_3, and heap_4 all support freeing memory, with heap_3 using standard malloc/free.
  3. Final Answer:

    heap_1 -> Option A
  4. Quick Check:

    heap_1 no free support = heap_1 [OK]
Hint: heap_1 never frees memory, others do [OK]
Common Mistakes:
  • Confusing heap_1 with heap_2 which supports free
  • Assuming all heaps support freeing memory
  • Mixing heap_3's malloc/free with heap_1's behavior
2. Which of the following is the correct way to include the heap_3 implementation in your FreeRTOS project?
easy
A. #include "heap3.c"
B. #include "heap_3.c"
C. #include "FreeRTOSHeap3.c"
D. #include

Solution

  1. Step 1: Check FreeRTOS heap file naming

    Heap implementations are included by adding the corresponding .c file, e.g., "heap_3.c".
  2. Step 2: Verify correct syntax

    Using #include "heap_3.c" is the standard way to include heap_3 in the build.
  3. Final Answer:

    #include "heap_3.c" -> Option B
  4. Quick Check:

    Include heap_3.c with quotes = D [OK]
Hint: Include heap source file with quotes, not angle brackets [OK]
Common Mistakes:
  • Using angle brackets instead of quotes
  • Incorrect file name spelling or casing
  • Trying to include a header instead of the .c file
3. Given the following code snippet using heap_4, what will be the output if the allocation fails?
void *ptr = pvPortMalloc(1000);
if(ptr == NULL) {
    printf("Allocation failed\n");
} else {
    printf("Allocation succeeded\n");
}
medium
A. Compilation error
B. Allocation succeeded
C. Allocation failed
D. No output

Solution

  1. Step 1: Understand heap_4 allocation behavior

    heap_4 supports allocation and freeing with coalescing. If memory is insufficient, pvPortMalloc returns NULL.
  2. Step 2: Analyze the if condition

    If ptr is NULL, the code prints "Allocation failed"; otherwise, it prints "Allocation succeeded".
  3. Final Answer:

    Allocation failed -> Option C
  4. Quick Check:

    pvPortMalloc NULL means failure = A [OK]
Hint: pvPortMalloc returns NULL on failure [OK]
Common Mistakes:
  • Assuming allocation always succeeds
  • Confusing NULL with zero pointer
  • Ignoring the if condition logic
4. You are using heap_2 in your FreeRTOS project but notice memory is not being freed properly. Which of the following is the most likely cause?
medium
A. heap_2 does not support freeing memory
B. heap_2 uses standard malloc/free which is not thread-safe
C. heap_2 requires manual coalescing of free blocks
D. You forgot to call vPortFree() after allocation

Solution

  1. Step 1: Recall heap_2 features

    heap_2 supports freeing memory via vPortFree(), but does not coalesce free blocks.
  2. Step 2: Identify common mistake

    If memory is not freed, the most common cause is forgetting to call vPortFree() after allocation.
  3. Final Answer:

    You forgot to call vPortFree() after allocation -> Option D
  4. Quick Check:

    Missing vPortFree causes leaks = B [OK]
Hint: Always call vPortFree() to release heap_2 memory [OK]
Common Mistakes:
  • Assuming heap_2 lacks free support
  • Confusing heap_2 with heap_1 or heap_3
  • Thinking heap_2 uses malloc/free internally
5. You want to use a FreeRTOS heap implementation that supports both allocation and freeing, minimizes fragmentation by coalescing adjacent free blocks, and is suitable for real-time systems. Which heap should you choose?
hard
A. heap_4
B. heap_2
C. heap_1
D. heap_5

Solution

  1. Step 1: Review heap features

    heap_1 does not support freeing; heap_2 supports free but no coalescing; heap_4 supports free and coalesces adjacent free blocks; heap_5 extends heap_4 with multiple memory regions.
  2. Step 2: Match requirements

    For minimizing fragmentation and real-time suitability, heap_4 is ideal as it coalesces free blocks and is deterministic.
  3. Final Answer:

    heap_4 -> Option A
  4. Quick Check:

    heap_4 coalesces free blocks = A [OK]
Hint: heap_4 coalesces free blocks, best for fragmentation [OK]
Common Mistakes:
  • Choosing heap_5 without needing multiple regions
  • Assuming heap_2 coalesces free blocks
  • Picking heap_1 which lacks free support
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