Memory Management Question Bank for C-CAT
Topic-wise Memory Management MCQs for CDAC C-CAT preparation with answers and explanations.
Show Answer & Explanation
Correct Answer: B - malloc()
malloc() (memory allocation) is used to allocate a block of memory dynamically from the heap.
Show Answer & Explanation
Correct Answer: D - calloc() initializes memory to zero
calloc() allocates memory and initializes all bytes to zero, while malloc() does not initialize the allocated memory.
Show Answer & Explanation
Correct Answer: A - free()
free() is used to deallocate memory that was previously allocated by malloc(), calloc(), or realloc().
Show Answer & Explanation
Correct Answer: D - Allocated memory not freed
A memory leak occurs when dynamically allocated memory is not freed, causing the program to consume more and more memory over time.
Show Answer & Explanation
Correct Answer: B - Resizes allocated memory
realloc() changes the size of previously allocated memory block, either expanding or shrinking it.
Show Answer & Explanation
Correct Answer: A - <stdlib.h>
Dynamic memory allocation functions are declared in <stdlib.h> header file.
Show Answer & Explanation
Correct Answer: A - void*
malloc() returns void* (generic pointer) which can be cast to any pointer type.
Show Answer & Explanation
Correct Answer: B - Returns NULL
If malloc() fails to allocate memory, it returns NULL. Always check for NULL before using the pointer.
Show Answer & Explanation
Correct Answer: D - Pointer to freed memory
A dangling pointer points to memory that has been freed or deallocated. Using it leads to undefined behavior.
Show Answer & Explanation
Correct Answer: D - Heap
Dynamic memory is allocated from the heap. Stack is used for local variables and function calls.
What is the output?
int *p = (int*)malloc(sizeof(int));
*p = 100;
free(p);
printf("%d", *p);Show Answer & Explanation
Correct Answer: C - Undefined behavior
After free(), accessing the memory through p is undefined behavior. The pointer becomes dangling.
Show Answer & Explanation
Correct Answer: C - 20 bytes
calloc(n, size) allocates n * size bytes. So 5 * 4 = 20 bytes are allocated.
Show Answer & Explanation
Correct Answer: B - Stack is faster, heap is dynamic
Stack memory is faster but limited in size and managed automatically. Heap is slower but dynamic and larger.
Show Answer & Explanation
Correct Answer: B - Nothing happens
Calling free(NULL) is safe and does nothing. It is defined behavior in C standard.
Show Answer & Explanation
Correct Answer: D - malloc(10 * sizeof(int))
malloc(10 * sizeof(int)) allocates enough bytes for 10 integers.
Show Answer & Explanation
Correct Answer: A - Calling free() twice on same pointer
Double free occurs when free() is called twice on the same memory address, causing undefined behavior.
Show Answer & Explanation
Correct Answer: C - Initializes memory to zero
calloc() initializes all allocated bytes to zero, while malloc() leaves memory uninitialized.
Show Answer & Explanation
Correct Answer: A - Frees the memory
realloc(ptr, 0) is equivalent to free(ptr) and frees the memory block. The behavior may vary by implementation.
Show Answer & Explanation
Correct Answer: B - Uninitialized pointer
A wild pointer is an uninitialized pointer that points to some arbitrary memory location.
Show Answer & Explanation
Correct Answer: A - Set pointer to NULL
After free(), set the pointer to NULL to avoid using dangling pointer accidentally.
Show Answer & Explanation
Correct Answer: C - malloc()
malloc() (memory allocation) is the standard C function for dynamic memory allocation. It allocates a specified number of bytes and returns a void pointer.
Show Answer & Explanation
Correct Answer: C - NULL
malloc() returns NULL if it cannot allocate the requested memory. Always check the return value before using the allocated memory.
Show Answer & Explanation
Correct Answer: B - calloc() initializes memory to zero, malloc() does not
malloc() allocates uninitialized memory, while calloc() allocates memory and initializes all bytes to zero. calloc() also takes the number and size of elements as separate parameters.
What is the output?
int *p = (int*)malloc(5 * sizeof(int));
for(int i = 0; i < 5; i++)
p[i] = (i + 1) * 10;
printf("%d", p[3]);
free(p);Show Answer & Explanation
Correct Answer: D - 40
The loop sets p[0]=10, p[1]=20, p[2]=30, p[3]=40, p[4]=50. p[3] is 40.
Show Answer & Explanation
Correct Answer: D - Deallocates previously allocated dynamic memory
free() deallocates memory that was previously allocated by malloc(), calloc(), or realloc(). The memory is returned to the heap for reuse.
Show Answer & Explanation
Correct Answer: A - Undefined behavior
Calling free() on a pointer not returned by malloc/calloc/realloc causes undefined behavior, which may result in crashes or memory corruption.
Show Answer & Explanation
Correct Answer: A - Dynamically allocated memory that is never freed
A memory leak occurs when dynamically allocated memory is never freed, making it unavailable for the rest of the program. Over time, this can exhaust available memory.
What does realloc() do?
int *p = malloc(3 * sizeof(int));
p = realloc(p, 5 * sizeof(int));Show Answer & Explanation
Correct Answer: D - Changes the size of previously allocated memory, preserving existing data
realloc() resizes previously allocated memory. If the block is expanded, existing data is preserved and the additional memory is uninitialized. It may move the block to a new location.
What is wrong with this code?
int *p = malloc(sizeof(int));
*p = 42;
free(p);
printf("%d", *p);Show Answer & Explanation
Correct Answer: D - Accessing memory after it has been freed (use-after-free)
After free(p), the memory pointed to by p is deallocated. Accessing *p after free is a use-after-free bug, which is undefined behavior.
Show Answer & Explanation
Correct Answer: B - A pointer that points to memory that has been freed
A dangling pointer is a pointer that refers to memory that has been deallocated (freed). Using it leads to undefined behavior.
What is the correct way to avoid a dangling pointer after free()?
int *p = malloc(sizeof(int));
free(p);Show Answer & Explanation
Correct Answer: B - Set p = NULL after free
Setting the pointer to NULL after freeing prevents accidental use-after-free bugs. Dereferencing NULL causes a predictable crash rather than subtle corruption.
Show Answer & Explanation
Correct Answer: B - 5 integers initialized to zero
calloc(5, sizeof(int)) allocates memory for 5 integers (5 * sizeof(int) bytes) and initializes all bits to zero.
What is the output?
int *p = calloc(3, sizeof(int));
printf("%d %d %d", p[0], p[1], p[2]);
free(p);Show Answer & Explanation
Correct Answer: D - 0 0 0
calloc() initializes all allocated memory to zero. So p[0], p[1], and p[2] are all 0.
What happens when realloc() is called with size 0?
int *p = malloc(10 * sizeof(int));
p = realloc(p, 0);Show Answer & Explanation
Correct Answer: C - Memory is freed (implementation-defined)
Calling realloc(p, 0) is implementation-defined. In many implementations it acts like free(p) and returns NULL, but this behavior is not guaranteed by the C standard.
What is wrong with this code?
int *p = malloc(5 * sizeof(int));
p = malloc(10 * sizeof(int));
free(p);Show Answer & Explanation
Correct Answer: B - The first allocation is leaked because p is reassigned
The first malloc allocates memory and p points to it. When p is reassigned to the second malloc, the first allocation becomes unreachable, causing a memory leak.
Show Answer & Explanation
Correct Answer: C - Heap
Dynamic memory allocated by malloc/calloc/realloc comes from the heap. The stack is used for local variables and function calls.
What is the output?
int *arr = malloc(3 * sizeof(int));
arr[0] = 10; arr[1] = 20; arr[2] = 30;
arr = realloc(arr, 5 * sizeof(int));
arr[3] = 40; arr[4] = 50;
printf("%d %d", arr[1], arr[4]);
free(arr);Show Answer & Explanation
Correct Answer: B - 20 50
realloc() preserves existing data (10, 20, 30) and extends the allocation. arr[1] is still 20, and arr[4] is set to 50.
Show Answer & Explanation
Correct Answer: A - Calling free() on the same pointer twice
A double free occurs when free() is called on the same memory address twice. This causes undefined behavior and can lead to crashes or security vulnerabilities.
What does this code demonstrate?
void func() {
int *p = malloc(100 * sizeof(int));
// function returns without calling free(p)
}Show Answer & Explanation
Correct Answer: C - A memory leak
The function allocates memory but never frees it. Since p is a local variable, it is destroyed when the function returns, making the allocated memory unreachable — a memory leak.
What is wrong with this realloc usage?
int *p = malloc(5 * sizeof(int));
p = realloc(p, 10 * sizeof(int));Show Answer & Explanation
Correct Answer: A - If realloc fails, the original memory is leaked
If realloc() fails, it returns NULL but does not free the original memory. Assigning directly to p loses the original pointer, causing a leak. Use a temporary pointer instead.
Show Answer & Explanation
Correct Answer: C - int *tmp = realloc(p, newsize); if(tmp) p = tmp;
Using a temporary pointer preserves the original pointer if realloc fails (returns NULL). Only update the original pointer after confirming success.
Show Answer & Explanation
Correct Answer: C - Stack memory is managed automatically; heap memory must be managed manually
Stack memory is allocated and deallocated automatically with function calls. Heap memory is allocated using malloc/calloc and must be freed explicitly by the programmer.
What does this code output?
char *s = malloc(6);
strcpy(s, "Hello");
printf("%s", s);
free(s);Show Answer & Explanation
Correct Answer: D - Hello
malloc(6) allocates 6 bytes. strcpy copies "Hello" (5 chars + null) into the buffer. printf outputs "Hello". Memory is properly freed.
Show Answer & Explanation
Correct Answer: A - A pointer that has not been initialized and contains a random address
A wild pointer is an uninitialized pointer that contains a random or garbage address. Dereferencing it causes undefined behavior.
What is the problem with this code?
int *p;
*p = 10;
printf("%d", *p);Show Answer & Explanation
Correct Answer: B - p is a wild pointer - it was never assigned a valid address
p is declared but never initialized or allocated. Writing *p = 10 dereferences a wild pointer, which is undefined behavior and likely causes a crash.
Show Answer & Explanation
Correct Answer: D - Implementation-defined: may return NULL or a unique pointer
malloc(0) has implementation-defined behavior. It may return NULL or a unique pointer that can be passed to free(). The result should not be dereferenced.
What does this code do?
int **matrix = malloc(3 * sizeof(int*));
for(int i = 0; i < 3; i++)
matrix[i] = malloc(4 * sizeof(int));Show Answer & Explanation
Correct Answer: A - Allocates a 3x4 integer matrix dynamically
This allocates a dynamically-sized 2D array (3 rows, 4 columns). First, an array of 3 int pointers is allocated, then each pointer is allocated an array of 4 ints.
What is the correct way to free the dynamically allocated 2D array?
int **matrix = malloc(3 * sizeof(int*));
for(int i = 0; i < 3; i++)
matrix[i] = malloc(4 * sizeof(int));Show Answer & Explanation
Correct Answer: A - Free each row first, then free matrix
You must free each row (matrix[i]) first, then free the array of pointers (matrix). Freeing in reverse order of allocation prevents memory leaks.
What does the following code print?
int *a = malloc(sizeof(int));
int *b = a;
*a = 100;
printf("%d", *b);
free(a);Show Answer & Explanation
Correct Answer: C - 100
Both a and b point to the same dynamically allocated memory. Setting *a = 100 changes the value at that location, so *b also reads 100.
Show Answer & Explanation
Correct Answer: C - Memory being split into small unused blocks due to repeated allocations and frees
Memory fragmentation occurs when free memory is broken into small, non-contiguous blocks after many allocations and deallocations, making it difficult to satisfy large allocation requests.