Bird
Raised Fist0
Cprogramming~20 mins

free function - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Free Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this code using free()?

Consider the following C code snippet. What will be printed when it runs?

C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr = malloc(sizeof(int));
    *ptr = 42;
    free(ptr);
    printf("%d\n", *ptr);
    return 0;
}
A0
BCompilation error
C42
DUndefined behavior (likely garbage or crash)
Attempts:
2 left
💡 Hint

Think about what happens to memory after free() is called.

🧠 Conceptual
intermediate
1:30remaining
What does free() do in C?

Which of the following best describes what the free() function does?

ADeallocates previously allocated memory
BCopies memory from one location to another
CAllocates memory on the heap
DInitializes memory to zero
Attempts:
2 left
💡 Hint

Think about how you return memory to the system after using malloc.

🔧 Debug
advanced
2:00remaining
Identify the error related to free() in this code

What error will this code cause when run?

C
#include <stdlib.h>

int main() {
    int x = 10;
    int *ptr = &x;
    free(ptr);
    return 0;
}
ASegmentation fault at malloc
BFreeing memory not allocated by malloc causes undefined behavior
CNo error, runs fine
DDouble free error
Attempts:
2 left
💡 Hint

Consider what kind of pointers can be passed to free().

📝 Syntax
advanced
1:30remaining
Which option correctly frees a dynamically allocated array?

Given int *arr = malloc(5 * sizeof(int));, which option correctly frees the memory?

Afree(arr[0]);
Bfree(&arr);
Cfree(arr); arr = NULL;
Dfree(*arr);
Attempts:
2 left
💡 Hint

Remember what free() expects as its argument.

🚀 Application
expert
2:30remaining
How many times is free() called and what is the output?

Analyze the code below. How many times is free() called and what is printed?

C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *a = malloc(sizeof(int));
    int *b = a;
    *a = 100;
    free(a);
    free(b);
    printf("%d\n", *b);
    return 0;
}
Afree() called twice; causes undefined behavior (likely crash)
Bfree() called twice; prints 100
Cfree() called once; prints 100
Dfree() called once; causes segmentation fault
Attempts:
2 left
💡 Hint

Consider what happens when you free the same memory twice.

Practice

(1/5)
1. What is the main purpose of the free function in C?
easy
A. To initialize a pointer to NULL
B. To allocate new memory dynamically
C. To copy data from one memory location to another
D. To release memory previously allocated by malloc or similar functions

Solution

  1. Step 1: Understand what free does

    The free function releases memory that was allocated dynamically, returning it to the system.
  2. Step 2: Differentiate from other memory functions

    malloc allocates memory, free releases it. Copying or initializing pointers is unrelated.
  3. Final Answer:

    To release memory previously allocated by malloc or similar functions -> Option D
  4. Quick Check:

    free releases memory = A [OK]
Hint: Remember: free returns memory, malloc gets memory [OK]
Common Mistakes:
  • Confusing free with malloc or calloc
  • Thinking free allocates memory
  • Using free to copy data
2. Which of the following is the correct way to free memory allocated by malloc?
easy
A. free(pointer); pointer = NULL;
B. free(pointer); free(pointer);
C. free(pointer); pointer++;
D. free(pointer); pointer = malloc(10);

Solution

  1. Step 1: Free the allocated memory

    Calling free(pointer); releases the memory correctly.
  2. Step 2: Avoid dangling pointer

    Setting pointer = NULL; after freeing prevents accidental use of freed memory.
  3. Final Answer:

    free(pointer); pointer = NULL; -> Option A
  4. Quick Check:

    Free then NULL pointer = D [OK]
Hint: Always set pointer to NULL after free to avoid bugs [OK]
Common Mistakes:
  • Calling free twice on the same pointer
  • Incrementing pointer after free
  • Reassigning pointer without freeing first
3. What will be the output of this C code?
char *ptr = malloc(5);
strcpy(ptr, "test");
free(ptr);
printf("%s", ptr);
medium
A. test
B. Undefined behavior (may print garbage or crash)
C. Compilation error
D. Empty string

Solution

  1. Step 1: Analyze memory allocation and free

    Memory is allocated and string "test" copied. Then memory is freed.
  2. Step 2: Using pointer after free

    Printing ptr after free causes undefined behavior; it may print garbage or crash.
  3. Final Answer:

    Undefined behavior (may print garbage or crash) -> Option B
  4. Quick Check:

    Using freed pointer = undefined behavior [OK]
Hint: Never use pointer after free without reassigning [OK]
Common Mistakes:
  • Assuming freed pointer still holds valid data
  • Expecting compilation error
  • Thinking it prints empty string
4. Identify the error in this code snippet:
int *arr = malloc(3 * sizeof(int));
free(arr + 1);
medium
A. free called on pointer not returned by malloc
B. Missing free call
C. Incorrect size in malloc
D. No error, code is correct

Solution

  1. Step 1: Understand pointer passed to free

    Memory was allocated at arr, but free is called on arr + 1, which is not the original pointer.
  2. Step 2: Free must be called on original pointer

    Only the pointer returned by malloc (or NULL) can be passed to free safely.
  3. Final Answer:

    free called on pointer not returned by malloc -> Option A
  4. Quick Check:

    Free must use original pointer = A [OK]
Hint: Free only original malloc pointer, not offset pointers [OK]
Common Mistakes:
  • Freeing pointer offset from malloc result
  • Not freeing allocated memory
  • Assuming free can take any pointer inside block
5. You have a function that reallocates memory for an integer array:
int *resize(int *arr, int new_size) {
    int *temp = realloc(arr, new_size * sizeof(int));
    if (temp == NULL) {
        free(arr);
        return NULL;
    }
    return temp;
}

What is the best practice to avoid memory leaks when using this function?
hard
A. Always free the original pointer before calling resize
B. Call free on the returned pointer immediately after resize
C. Assign the return value to a temporary pointer before updating original pointer
D. Do not check if realloc returns NULL

Solution

  1. Step 1: Understand realloc behavior

    If realloc fails, it returns NULL and original memory remains allocated.
  2. Step 2: Use temporary pointer to avoid losing original memory

    Assign realloc result to a temp pointer first; if NULL, free original to avoid leaks.
  3. Final Answer:

    Assign the return value to a temporary pointer before updating original pointer -> Option C
  4. Quick Check:

    Use temp pointer for realloc to prevent leaks = B [OK]
Hint: Use temp pointer for realloc result before replacing original [OK]
Common Mistakes:
  • Overwriting original pointer without checking realloc result
  • Freeing original pointer before realloc
  • Ignoring NULL return from realloc
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