Bird
Raised Fist0
Cprogramming~20 mins

Memory allocation flow - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Memory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of malloc and free usage
What is the output of this C program that allocates and frees memory?
C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr = (int *)malloc(sizeof(int));
    if (ptr == NULL) {
        printf("Allocation failed\n");
        return 1;
    }
    *ptr = 42;
    printf("Value: %d\n", *ptr);
    free(ptr);
    return 0;
}
AValue: 42
BValue: 0
CSegmentation fault
DCompilation error
Attempts:
2 left
💡 Hint
Remember malloc returns a pointer to allocated memory which you can use before freeing it.
🧠 Conceptual
intermediate
1:30remaining
Understanding realloc behavior
What happens when you call realloc with a NULL pointer in C?
AIt frees the memory and returns NULL.
BIt causes a segmentation fault.
CIt behaves like malloc and allocates new memory.
DIt returns the original pointer without changes.
Attempts:
2 left
💡 Hint
Think about realloc's behavior when given a NULL pointer.
🔧 Debug
advanced
2:30remaining
Identify the cause of memory leak
What causes the memory leak in this code snippet?
C
#include <stdlib.h>

void func() {
    int *arr = malloc(10 * sizeof(int));
    arr = malloc(20 * sizeof(int));
    free(arr);
}
AThere is no memory leak; code is correct.
BThe first malloc'd memory is overwritten without free, causing a leak.
Cfree is called twice on the same pointer.
DThe second malloc is invalid and causes a crash.
Attempts:
2 left
💡 Hint
Look at what happens to the pointer after the first malloc.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in memory allocation
Which option contains a syntax error in memory allocation?
C
int *ptr = malloc(sizeof(int) * 5);
Aint *ptr = malloc(sizeof(int) * 5);
Bint *ptr = (int *)malloc(sizeof(int) * 5);
Cint *ptr = malloc(sizeof(int) * 5);;
Dint *ptr = malloc(sizeof(int) * 5)
Attempts:
2 left
💡 Hint
Check for missing semicolons or extra characters.
🚀 Application
expert
3:00remaining
Predict the number of allocated blocks
After running this code, how many distinct memory blocks are allocated and not freed?
C
#include <stdlib.h>

int main() {
    int *a = malloc(10 * sizeof(int));
    int *b = malloc(5 * sizeof(int));
    free(a);
    a = malloc(20 * sizeof(int));
    int *c = realloc(b, 15 * sizeof(int));
    return 0;
}
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Count allocations minus frees carefully, consider realloc behavior.

Practice

(1/5)
1. What does the malloc function do in C programming?
easy
A. Frees previously allocated memory
B. Allocates a block of memory on the heap
C. Declares a variable
D. Copies data from one memory location to another

Solution

  1. Step 1: Understand the purpose of malloc

    malloc is used to allocate memory dynamically on the heap during program execution.
  2. Step 2: Differentiate from other memory functions

    free releases memory, declaration creates variables, and copying data is done by functions like memcpy.
  3. Final Answer:

    Allocates a block of memory on the heap -> Option B
  4. Quick Check:

    malloc = Allocate memory [OK]
Hint: Remember: malloc = get memory, free = release memory [OK]
Common Mistakes:
  • Confusing malloc with free
  • Thinking malloc declares variables
  • Assuming malloc copies data
2. Which of the following is the correct syntax to allocate memory for 10 integers using malloc?
easy
A. int *ptr = malloc(sizeof(int));
B. int *ptr = malloc(10);
C. int ptr = malloc(10 * sizeof(int));
D. int *ptr = malloc(10 * sizeof(int));

Solution

  1. Step 1: Calculate total memory needed

    To allocate memory for 10 integers, multiply 10 by the size of an int: 10 * sizeof(int).
  2. Step 2: Check pointer type and assignment

    The pointer must be of type int * and assigned the result of malloc. int *ptr = malloc(10 * sizeof(int)); matches this.
  3. Final Answer:

    int *ptr = malloc(10 * sizeof(int)); -> Option D
  4. Quick Check:

    Correct malloc size and pointer type = int *ptr = malloc(10 * sizeof(int)); [OK]
Hint: Multiply count by sizeof(type) for malloc size [OK]
Common Mistakes:
  • Not multiplying by sizeof(int)
  • Using wrong pointer type
  • Allocating too little memory
3. What will be the output of this C code snippet?
int *ptr = malloc(4 * sizeof(int));
if (ptr == NULL) {
    printf("Allocation failed\n");
} else {
    ptr[0] = 10;
    ptr[1] = 20;
    printf("%d %d\n", ptr[0], ptr[1]);
    free(ptr);
}
medium
A. 10 20
B. 0 0
C. Allocation failed
D. Compilation error

Solution

  1. Step 1: Check memory allocation and NULL condition

    The code allocates memory for 4 integers. If allocation fails, it prints "Allocation failed". Otherwise, it proceeds.
  2. Step 2: Assign and print values

    Since allocation likely succeeds, ptr[0] = 10 and ptr[1] = 20 are set, then printed as "10 20".
  3. Final Answer:

    10 20 -> Option A
  4. Quick Check:

    Successful malloc prints assigned values [OK]
Hint: Check if malloc returns NULL before using pointer [OK]
Common Mistakes:
  • Ignoring NULL check
  • Assuming uninitialized values print zero
  • Forgetting to free memory
4. Identify the error in this code snippet related to memory allocation:
char *str = malloc(5);
strcpy(str, "Hello World");
free(str);
medium
A. Memory allocated is too small for the string
B. Missing check if malloc returned NULL
C. Using strcpy without including
D. No error, code is correct

Solution

  1. Step 1: Check allocated memory size

    Memory allocated is 5 bytes, but the string "Hello World" needs 12 bytes (11 chars + 1 null terminator).
  2. Step 2: Understand consequences of insufficient memory

    Copying a larger string into smaller allocated space causes buffer overflow and undefined behavior.
  3. Final Answer:

    Memory allocated is too small for the string -> Option A
  4. Quick Check:

    Allocated size < string length = Memory allocated is too small for the string [OK]
Hint: Allocate enough space including null terminator [OK]
Common Mistakes:
  • Ignoring string length vs allocated size
  • Not checking malloc return value
  • Assuming strcpy auto-expands memory
5. You want to allocate memory for an array of 5 integers, initialize them to zero, and then safely release the memory. Which code snippet correctly implements this flow?
hard
A. int *arr = malloc(5 * sizeof(int)); for(int i=0; i<5; i++) arr[i] = 0;
B. int arr[5]; for(int i=0; i<5; i++) arr[i] = 0;
C. int *arr = calloc(5, sizeof(int)); free(arr);
D. int *arr = malloc(5); memset(arr, 0, 5 * sizeof(int)); free(arr);

Solution

  1. Step 1: Understand memory allocation and initialization

    calloc allocates and initializes memory to zero automatically, perfect for this task.
  2. Step 2: Check safe memory release

    free(arr); correctly releases the allocated memory after use.
  3. Final Answer:

    int *arr = calloc(5, sizeof(int)); free(arr); -> Option C
  4. Quick Check:

    calloc zero-initializes and malloc does not [OK]
Hint: Use calloc for zero-initialized memory allocation [OK]
Common Mistakes:
  • Using malloc without initializing memory
  • Allocating too little memory
  • Not freeing dynamically allocated memory
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