C Dynamic Memory Allocation - Tricky MCQ

Previous C Dynamic Memory Allocation Next

Tricky Questions on Dynamic Memory Allocation

1

What is the difference between malloc() and calloc()?

Correct Answer: D) All of the above

malloc() allocates raw, uninitialized memory and takes one argument (size in bytes). calloc() allocates memory for an array of elements, initializes all bits to zero, and takes two arguments (number of elements, size of each element). calloc() is generally slower because it writes zero to all allocated bytes, but ensures predictable initial state.

2

What happens if you call free() on a NULL pointer?

Correct Answer: C) Nothing - free(NULL) is safe and does nothing

The C standard specifies that free(NULL) does nothing and is safe to call. This allows code to call free() on pointers that might be NULL without checking first. However, calling free() on an already freed pointer (double free) or on a pointer not returned by malloc/calloc/realloc causes undefined behavior.

3

What is memory fragmentation and how does it affect dynamic allocation?

Correct Answer: D) All of the above

Memory fragmentation occurs when free memory is broken into small, non-contiguous blocks. External fragmentation happens between allocated blocks; internal fragmentation occurs within allocated blocks (wasted space due to allocation granularity). Fragmentation can cause allocation failures for large requests even when total free memory appears sufficient, especially with frequent allocation/deallocation of varying sizes.

4

What is the return type of malloc() and what does it return on failure?

Correct Answer: A) void* - returns NULL on failure

malloc() returns void* (generic pointer) which can be cast to any pointer type. On successful allocation, it returns a pointer to the allocated memory. On failure (insufficient memory), it returns NULL. Always check the return value of malloc() before using the pointer to avoid dereferencing NULL, which causes undefined behavior (typically segmentation fault).

5

What is a memory leak and how can it be detected?

Correct Answer: D) All of the above

A memory leak occurs when dynamically allocated memory is not freed after it's no longer needed, causing the program to gradually consume more memory. This can lead to system slowdowns or crashes when memory is exhausted. Memory leaks are detected using specialized tools like Valgrind, AddressSanitizer, or built-in heap profilers. In C, there's no automatic garbage collection, so programmers must manually manage memory.

6

What does realloc() do and what are its possible return values?

Correct Answer: D) All of the above

realloc() changes the size of a previously allocated memory block. It can: 1) Extend/shrink in place if adjacent memory is free (returns same pointer), 2) Allocate new block, copy data, free old block (returns new pointer), 3) Fail (returns NULL). Old content is preserved up to the minimum of old and new size. If realloc() fails, the original block remains unchanged. Always use a temporary pointer with realloc() to avoid losing the original pointer on failure.

7

What is the difference between stack and heap memory?

Correct Answer: D) All of the above

Stack memory is used for automatic variables (local variables, function parameters), follows LIFO order, has limited size (can cause stack overflow), and allocation/deallocation is automatic and fast. Heap memory is used for dynamic allocation via malloc/calloc/realloc, requires manual management with free(), is larger but slower, can fragment, and allows flexible sizing. Stack memory is thread-specific; heap is shared within process.

8

What is a dangling pointer?

Correct Answer: A) Pointer pointing to freed memory

A dangling pointer is a pointer that points to memory that has been freed. Using a dangling pointer causes undefined behavior (reading garbage data, writing to freed memory, or segmentation fault). Common causes: 1) Returning pointer to local variable, 2) Accessing memory after free(), 3) Multiple pointers to same memory where one frees it. Solution: Set pointer to NULL after free() and check before use.

9

What is the purpose of the sizeof operator in dynamic allocation?

Correct Answer: D) All of the above

The sizeof operator returns the size in bytes of a type or variable. In dynamic allocation, it's crucial for: 1) Calculating correct allocation size (e.g., malloc(n * sizeof(int))), 2) Ensuring portability since type sizes vary across platforms (int is 4 bytes on 32-bit, may be different elsewhere), 3) Avoiding hardcoded sizes that break if type size changes. sizeof is evaluated at compile-time, not runtime.

10

What happens if you allocate zero bytes with malloc(0)?

Correct Answer: D) Both B and C

According to the C standard, malloc(0) returns either a NULL pointer or a unique non-NULL pointer that cannot be dereferenced (attempting to do so causes undefined behavior). The behavior is implementation-defined. The returned pointer (if non-NULL) can be passed to free(). This is mostly useful for edge cases in algorithms. In practice, avoid malloc(0) as it's confusing and implementation-dependent.

11

What is alignment in memory allocation and why is it important?

Correct Answer: D) All of the above

Memory alignment means data is stored at addresses divisible by specific values (typically 2, 4, 8, or 16). CPUs access aligned data more efficiently; some architectures require alignment for certain types. Misaligned access can cause performance penalties (slowdown) or hardware exceptions (crashes on some architectures). malloc() returns memory suitably aligned for any data type. For custom alignment needs, use aligned_alloc() (C11) or platform-specific functions.

12

What is the difference between free() and deleting memory in other languages?

Correct Answer: D) All of the above

free() in C merely marks memory as available for future allocations; it doesn't overwrite or clear the memory. C has no automatic garbage collection - programmers must manually free memory. Unlike delete in C++ or garbage collection in Java/Python, free() doesn't call destructors or finalizers. After free(), the memory content may remain unchanged until overwritten by subsequent allocations, which is a security risk if sensitive data was stored.

13

What is wild pointer and how is it different from dangling pointer?

Correct Answer: D) All of the above

A wild pointer is uninitialized and contains a garbage address (random value). A dangling pointer was initialized but now points to freed memory. Both cause undefined behavior when dereferenced. Wild pointers are created by declaring pointer without initialization; dangling pointers by freeing memory without nullifying the pointer. Solution: Always initialize pointers to NULL and set to NULL after free().

14

Can you free() memory allocated with alloca()?

Correct Answer: B) No, alloca() allocates on stack, automatically freed

alloca() allocates memory on the stack (not heap), which is automatically freed when the function returns. You should not call free() on alloca() memory. alloca() is non-standard (GCC/Clang extension), can cause stack overflow if large allocations are made, and the memory disappears when the function returns (dangerous if pointer is returned). Unlike malloc(), alloca() doesn't return NULL on failure - it may crash with stack overflow.

15

What is memory pooling and why is it used?

Correct Answer: D) All of the above

Memory pooling (object pooling) pre-allocates a large block of memory and divides it into fixed-size chunks. When an allocation is needed, a chunk is taken from the pool; when freed, it's returned to the pool. Benefits: 1) Reduces fragmentation (all chunks same size), 2) Faster than general-purpose malloc/free (no complex heap management), 3) Predictable performance, 4) Good for frequent allocations of same-sized objects. Used in real-time systems, game engines, and embedded systems.

Previous C Dynamic Memory Allocation Next