"Hello World" is 11 bytes (including null terminator). Writing to 10-byte buffer causes buffer overflow, corrupting adjacent memory.
Advertisement
Q.66Hard
For implementing a dynamic stack in competitive programming, which memory management approach is best?
Answer: B
Single allocation with index tracking minimizes malloc/free overhead, suitable for competitive programming time constraints.
Q.67Easy
Which function in C is used to allocate memory dynamically at runtime?
Answer: A
malloc() is the standard C library function for dynamic memory allocation. It takes the number of bytes to allocate as an argument and returns a pointer to the allocated memory.
Q.68Easy
Which header file must be included to use malloc() and free() functions?
Answer: A
<stdlib.h> is the standard header file containing declarations for malloc(), calloc(), realloc(), and free() functions.
Q.69Easy
What happens when malloc() fails to allocate memory?
Answer: A
When malloc() cannot allocate the requested memory, it returns NULL. The programmer must check for NULL before using the pointer to avoid undefined behavior.
Q.70Medium
In a competitive programming problem, you need to allocate a 2D array of size 5x5 dynamically. Which approach is most efficient?
Answer: A
Allocating a single contiguous block is more cache-efficient and requires only one malloc() call, making it better for competitive programming where memory access speed matters.
Q.71Medium
What is the critical issue with this code snippet?
int *ptr = (int*)malloc(10);
ptr[11] = 5;
Answer: A
malloc(10) allocates 10 bytes, but accessing ptr[11] goes beyond the allocated boundary, causing a buffer overflow which leads to undefined behavior and potential segmentation fault.
Q.72Medium
Which of the following is a memory leak?
Answer: A
In option A, the first malloc(100) block becomes inaccessible after p is reassigned, but it's not freed, causing a memory leak. The original 100 bytes are lost.
Q.73Medium
In the 2024-2025 competitive exam pattern, a dynamic array needs to grow from 100 to 200 elements. Using realloc(), what should be the concern?
Answer: A
If realloc() needs to relocate the memory block to a different address, all old pointers to the array become invalid. Only the returned pointer should be used.
Q.74Easy
What is the result of calling free() on a NULL pointer?
Answer: A
Calling free(NULL) is safe in C and does nothing. This is by design, allowing you to write code like free(ptr) without checking if ptr is NULL first.
Q.75Medium
Which scenario represents a use-after-free error?
Answer: A
In option A, after free(p), the memory is deallocated. Accessing it with p[0] = 5 is a use-after-free error, leading to undefined behavior.
Q.76Medium
For storing variable-length strings dynamically in a competitive program, what's the safest approach?
Answer: A
Strings in C are null-terminated, so allocating strlen(s)+1 bytes ensures space for the null terminator. This is the standard safe approach.
Q.77Medium
What potential issue occurs with this code in a large-scale system?
while(1) { int *arr = malloc(1000000); }
Answer: A
This creates an infinite loop that allocates 1MB each iteration without freeing, rapidly exhausting available memory. This is a classic memory leak scenario.
Q.78Medium
In graph algorithms with dynamic adjacency lists, what's the memory requirement for a sparse graph with V vertices and E edges?
Answer: A
Using dynamic adjacency lists, you allocate V list pointers and E edge nodes total, giving O(V + E) space complexity, which is optimal for sparse graphs.
Q.79Medium
What is the advantage of dynamic allocation over static allocation in competitive programming?
Answer: A
Dynamic allocation allows using memory proportional to actual input size, preventing TLE from stack overflow with large static arrays and enabling solutions for variable-sized problems.
Q.80Medium
Which function should be used to allocate memory for an array of 50 structures of size 'sizeof(struct Node)' and initialize to zero?
Answer: A
calloc(50, sizeof(struct Node)) allocates memory for 50 elements of struct Node size and initializes all bytes to zero, which is ideal when you need initialized memory.