Test your understanding of C memory management with these 19 multiple-choice questions — malloc and free, dangling pointers, memory leaks, and stack versus heap. Every question has the answer and a short explanation hidden below it, and the trickier ones link to a full walkthrough with compilable code.
These questions come from our free Android app, C Programming Quiz — 150+ questions across 9 categories with score tracking, if you prefer to practise on your phone.
How to use this page: answer each question yourself before tapping Show answer. If you get one wrong, follow the linked explanation — knowing why the wrong options are wrong is what interviews test.
Question 1 of 19 (Easy)
Which function allocates a block of memory on the heap?
- malloc
- alloc
- new
- create
Show answer
Answer: A — malloc
malloc (and calloc/realloc) allocate dynamic memory on the heap. 'new' is C++, not C.
Question 2 of 19 (Medium)
What does malloc return if it cannot allocate the requested memory?
- NULL
- The requested size
- A valid pointer anyway
- -1
Show answer
Answer: A — NULL
On failure malloc returns a null pointer (NULL). Always check the return value before using it.
Question 3 of 19 (Medium)
Which header declares malloc and free?
- stdio.h
- stdlib.h
- string.h
- memory.h
Show answer
Answer: B — stdlib.h
malloc, calloc, realloc and free are declared in <stdlib.h>.
Question 4 of 19 (Medium)
When does a memory leak occur?
- When you read past an array
- When dynamically allocated memory is never freed
- When you free memory twice
- When a pointer is set to NULL
Show answer
Answer: B — When dynamically allocated memory is never freed
A memory leak happens when heap memory is allocated but never released, so it remains reserved and unusable until the program exits.
Question 5 of 19 (Medium)
How does calloc differ from malloc?
- It is faster
- It initializes the allocated memory to zero
- It allocates on the stack
- It never fails
Show answer
Answer: B — It initializes the allocated memory to zero
calloc allocates memory and initializes every byte to zero. malloc leaves the contents uninitialized (indeterminate).
Question 6 of 19 (Hard)
After 'free(p);', what best describes pointer p?
- It is automatically set to NULL
- It becomes a dangling pointer
- It still safely points to valid memory
- It is reused for the next malloc
Show answer
Answer: B — It becomes a dangling pointer
free releases the memory but does not change p. p now points to freed memory — a dangling pointer. Using it is undefined behavior; set it to NULL to be safe.
Question 7 of 19 (Hard)
What is the output of this code?
int *p = (int *)malloc(sizeof(int));
*p = 99;
free(p);
printf("%d", *p);
- 99
- 0
- NULL
- Undefined behavior
Show answer
Answer: D — Undefined behavior
After free(p), p is a dangling pointer. Reading through it is undefined behavior — the memory may have been reused or overwritten.
Full walkthrough with compilable code: read the detailed explanation.
Question 8 of 19 (Medium)
What does realloc(ptr, 0) do?
- Has no effect
- Is equivalent to free(ptr) and returns NULL or a unique pointer
- Allocates 0 bytes and crashes
- Doubles the existing allocation
Show answer
Answer: B — Is equivalent to free(ptr) and returns NULL or a unique pointer
realloc with size 0 frees the old block. The behavior (NULL or a unique freeable pointer) is implementation-defined, but not undefined. Treat the return as non-usable.
Question 9 of 19 (Hard)
What is a double-free error?
- Allocating memory twice
- Calling free on the same pointer twice
- Not freeing memory at all
- Using a freed pointer to allocate again
Show answer
Answer: B — Calling free on the same pointer twice
Calling free twice on the same pointer corrupts the heap allocator's internal state, causing undefined behavior — often a crash or security vulnerability.
Question 10 of 19 (Medium)
How many bytes does 'malloc(5 * sizeof(int))' allocate on a platform where sizeof(int) == 4?
- 5
- 10
- 20
- Depends on alignment
Show answer
Answer: C — 20
malloc requests 5 * 4 = 20 bytes to hold 5 int values.
Question 11 of 19 (Easy)
Where are local variables stored by default?
- Heap
- Stack
- Data segment
- Code segment
Show answer
Answer: B — Stack
Local (automatic) variables are allocated on the call stack. They are created when the function is entered and destroyed when it returns.
Question 12 of 19 (Hard)
What is the output of this code?
int *f(void) {
int x = 42;
return &x;
}
printf("%d", *f());
- 42
- 0
- Undefined behavior
- Compile error
Show answer
Answer: C — Undefined behavior
f returns the address of a local variable. Once f returns, x is destroyed and the address is dangling. Dereferencing it is undefined behavior.
Full walkthrough with compilable code: read the detailed explanation.
Question 13 of 19 (Medium)
What is stack overflow?
- Writing beyond the end of a heap allocation
- Exceeding the stack's capacity, usually through deep recursion
- Allocating too many global variables
- Using too much heap memory
Show answer
Answer: B — Exceeding the stack's capacity, usually through deep recursion
The call stack has a fixed size. Deep or infinite recursion keeps pushing frames until the stack is exhausted, causing a stack overflow (typically a segfault).
Question 14 of 19 (Hard)
What is an off-by-one error in array access?
- Starting an array at index 1 instead of 0
- Accessing index n when valid indices are 0 to n-1
- Forgetting to initialize array elements
- Passing an array to a function by value
Show answer
Answer: B — Accessing index n when valid indices are 0 to n-1
An off-by-one error accesses one element past the end (index n of an n-element array). It is undefined behavior and a common source of bugs and security vulnerabilities.
Question 15 of 19 (Medium)
What does memset(buf, 0, size) do?
- Allocates a zeroed buffer of 'size' bytes
- Sets every byte of 'buf' to 0 for 'size' bytes
- Copies 'size' zeros to buf from dev/zero
- Frees 'buf' and sets it to NULL
Show answer
Answer: B — Sets every byte of 'buf' to 0 for 'size' bytes
memset fills a region of memory with a specified byte value. memset(buf, 0, size) zeros out 'size' bytes starting at 'buf'.
Question 16 of 19 (Hard)
What is a buffer overflow?
- Allocating too much heap memory
- Writing data beyond the end of an allocated buffer
- Calling free too many times
- Reading an uninitialized variable
Show answer
Answer: B — Writing data beyond the end of an allocated buffer
A buffer overflow writes past the end of an array or allocated buffer, corrupting adjacent memory. It is a leading cause of security vulnerabilities.
Question 17 of 19 (Easy)
Which segment of memory stores global and static variables?
- Stack
- Heap
- Data segment
- Code segment
Show answer
Answer: C — Data segment
Global and static variables are stored in the data segment. Initialized globals go into .data; uninitialized (zero-initialized) globals go into .bss.
Question 18 of 19 (Medium)
What does memcpy(dest, src, n) do?
- Compares n bytes of dest and src
- Copies n bytes from src to dest (no overlap allowed)
- Moves n bytes handling overlapping regions
- Zeroes n bytes starting at dest
Show answer
Answer: B — Copies n bytes from src to dest (no overlap allowed)
memcpy copies exactly n bytes from src to dest. The regions must not overlap — use memmove for overlapping regions.
Question 19 of 19 (Hard)
What is the output of this code?
int a[3] = {1, 2, 3};
int *p = a;
p++;
free(p);
- No output — runs fine
- Crash or undefined behavior
- Frees a[1] cleanly
- Compile error
Show answer
Answer: B — Crash or undefined behavior
You can only pass to free() a pointer returned by malloc/calloc/realloc. p is now a+1 (pointing into the middle of a stack array), not a heap allocation. free(p) is undefined behavior.
Full walkthrough with compilable code: read the detailed explanation.
How Did You Score?
All 19 correct means this topic is interview-ready — try the harder timed rounds in the C Programming Quiz app. Missed a few? The guides below cover everything these questions test.
Keep Practising
- Use-After-Free in C
- Find Memory Leaks in C with Valgrind
- Pointers in C — A Complete Guide
- All quiz question walkthroughs
More C Quizzes
- C Basics Quiz — 28 Questions with Answers
- C Operators Quiz — 20 Questions with Answers
- C Pointers Quiz — 18 Questions with Answers
- C Arrays and Strings Quiz — 18 Questions with Answers
- C Functions Quiz — 15 Questions with Answers
- C Structs and Unions Quiz — 13 Questions with Answers
- C File I/O Quiz — 13 Questions with Answers
- C Preprocessor Quiz — 11 Questions with Answers