Showing 31–40 of 198 questions
What is a potential issue when allocating very large contiguous blocks (>10^8 bytes) dynamically?
A
malloc() always succeeds for any size
B
Fragmentation and allocation failure, should check return value
C
Integer overflow in size calculation
D
Free space is automatically allocated
Correct Answer:
B. Fragmentation and allocation failure, should check return value
EXPLANATION
Large allocations may fail due to heap fragmentation or insufficient contiguous memory. Always check malloc() return for NULL.
What is the typical behavior if malloc() is called in a loop without corresponding free() calls?
A
Program automatically clears memory
B
Memory leaks accumulate, eventually causing program crash
C
Compiler optimizes and prevents leaks
D
Memory is recycled automatically
Correct Answer:
B. Memory leaks accumulate, eventually causing program crash
EXPLANATION
Without freeing, allocated memory accumulates until system runs out of heap space, causing allocation failure or crash.
For a program handling 10^6 integers dynamically, which allocation is most appropriate?
A
int arr[1000000];
B
int *arr = malloc(1000000 * sizeof(int));
C
static int arr[1000000];
D
auto int arr[1000000];
Correct Answer:
B. int *arr = malloc(1000000 * sizeof(int));
EXPLANATION
Stack allocation (option A) will cause stack overflow. Dynamic allocation on heap is required for large data.
What distinguishes a dangling pointer?
A
A pointer that points to deallocated memory
B
A pointer initialized to NULL
C
A pointer that hasn't been malloc'd
D
A pointer that points to stack variable
Correct Answer:
A. A pointer that points to deallocated memory
EXPLANATION
Dangling pointer points to memory that has been freed. Accessing it causes undefined behavior.
Consider this code:
int *p = malloc(sizeof(int));
int *q = p;
free(p);
q[0] = 5; // What is the result?
A
Valid assignment
B
Compiler error
C
Use-after-free: undefined behavior
D
Memory automatically reallocated
Correct Answer:
C. Use-after-free: undefined behavior
EXPLANATION
Both p and q point to freed memory. Writing to q after free(p) is undefined behavior.
What is the risk in this code?
char *str = malloc(5);
strcpy(str, "Hello World");
A
Memory leak (str not freed)
B
Buffer overflow - writing 11 characters into 5 bytes
C
strcpy() is deprecated
D
Both A and B
Correct Answer:
D. Both A and B
EXPLANATION
Buffer overflow occurs (11 chars into 5 bytes), and memory is never freed, causing a leak.
What is a common mistake in this code?
void func() {
int *ptr;
ptr = malloc(sizeof(int) * 5);
func2(ptr);
free(ptr);
}
void func2(int *p) {
free(p);
}
A
Double free error - memory freed in both functions
B
Memory leak - malloc without free
C
Buffer overflow
D
Segmentation fault guaranteed
Correct Answer:
A. Double free error - memory freed in both functions
EXPLANATION
Memory is freed in func2(), then again in func(), causing double free error.
What does this realloc() call do?
int *p = malloc(5 * sizeof(int));
p = realloc(p, 10 * sizeof(int));
A
Allocates new 10-integer block, discards old data
B
Expands existing block to 10 integers, preserves old data
C
Frees old block and allocates new one
D
Returns error
Correct Answer:
B. Expands existing block to 10 integers, preserves old data
EXPLANATION
realloc() resizes the memory block while preserving existing data. If expansion in-place fails, it allocates new block and copies data.
What is the correct way to allocate memory for 2D dynamic array (3x4)?
A
int arr[3][4] = malloc(...);
B
int **arr = malloc(3 * sizeof(int*)); for each row: malloc(4 * sizeof(int));
C
int *arr = malloc(12 * sizeof(int));
D
int arr = malloc(3*4*sizeof(int));
Correct Answer:
B. int **arr = malloc(3 * sizeof(int*)); for each row: malloc(4 * sizeof(int));
EXPLANATION
For true 2D dynamic array, allocate pointer array first, then allocate each row. Option C is 1D linear allocation.
What is the issue in this code?
void func() {
int *p = malloc(sizeof(int));
*p = 5;
}
int main() {
func();
// p is not accessible here
return 0;
}
A
Syntax error
B
Memory leak - allocated memory is never freed
C
Stack overflow
D
No issue
Correct Answer:
B. Memory leak - allocated memory is never freed
EXPLANATION
The pointer p is local to func(). Memory is allocated but never freed, causing a memory leak.