How much memory in bytes does calloc(10, 4) allocate?
calloc(num, size) allocates num * size = 10 * 4 = 40 bytes total.
Which scenario best demonstrates a memory leak?
Original malloc(10) pointer is lost before freeing. The 10 bytes are now inaccessible - a memory leak.
What is the output of this code?
int *p = (int*)calloc(3, sizeof(int));
printf("%d %d %d", p[0], p[1], p[2]);
calloc() initializes all allocated memory to zero, so all elements will be 0.
If realloc() cannot expand memory at the current location, what does it do?
realloc() finds new space if needed, copies data, deallocates old block, and returns new pointer.
Consider this code:
int *p = malloc(sizeof(int));
int *q = p;
free(p);
q[0] = 5; // What is the result?
Both p and q point to freed memory. Writing to q after free(p) is undefined behavior.
Advertisement
In competitive exams, why is dynamic allocation preferred over static arrays for unknown input sizes?
Dynamic allocation allocates memory on heap based on runtime input, preventing fixed-size limitations and stack overflow.
What is the correct way to allocate a dynamic array of struct for 'n' elements?
struct Node { int data; char name[20]; };
Must allocate n * sizeof(struct Node) bytes to hold all n structures.
What distinguishes a dangling pointer?
Dangling pointer points to memory that has been freed. Accessing it causes undefined behavior.
For a program handling 10^6 integers dynamically, which allocation is most appropriate?
Stack allocation (option A) will cause stack overflow. Dynamic allocation on heap is required for large data.
What is the typical behavior if malloc() is called in a loop without corresponding free() calls?
Without freeing, allocated memory accumulates until system runs out of heap space, causing allocation failure or crash.
Which header file must be included to use dynamic memory allocation functions in C?
stdlib.h contains declarations for malloc(), calloc(), realloc(), and free() functions.
What does free() function do in C?
free() is used to deallocate memory that was previously allocated using malloc(), calloc(), or realloc().
What is the primary difference between malloc() and calloc()?
calloc() allocates memory and initializes all bytes to zero, while malloc() leaves the allocated memory uninitialized.
Which of the following correctly allocates memory for an array of 5 integers and initializes to zero?
calloc(5, sizeof(int)) allocates memory for 5 integers and initializes all to zero.
A competitive programmer allocates a 2D array dynamically. What is the correct approach?
2D arrays are allocated as array of pointers, where each row pointer points to a dynamically allocated array.
Consider a program that repeatedly allocates memory in a loop but never frees it. What problem occurs?
Allocating without freeing causes memory leak, where allocated memory is not returned to the system, eventually exhausting heap.
What happens if you call free() twice on the same pointer?
Double free is undefined behavior and can cause program crash or corruption. After first free(), the pointer should not be used.
In a competitive programming scenario, you need to store strings dynamically. What is safe practice?
Safe practice includes allocating exact size (input length + 1 for null terminator), checking for NULL, and using bounded string functions.
A program uses realloc() to expand an array from 100 to 200 elements. The old address is p. After realloc():?
realloc() returns a new pointer (old location may move). Must use: p = realloc(p, newsize) and check for NULL.
For a graph with V vertices and E edges stored dynamically, what is typical space complexity?
Adjacency list uses dynamic allocation proportional to V+E, while matrix uses fixed V² space regardless of actual edges.