What is the output of this code?
int *p = calloc(3, sizeof(int));
printf("%d %d %d", p[0], p[1], p[2]);
calloc() initializes all allocated bytes to 0, so all integers are 0.
Consider reallocating memory:
int *p = malloc(10);
p = realloc(p, 20);
realloc() extends the existing allocation, preserving original data, and may return the same or different address.
Which allocation method is most suitable for a dynamically growing linked list?
Each node should be allocated individually with malloc() to allow dynamic growth and efficient memory usage.
How can you safely check if malloc() succeeded?
malloc() returns NULL (pointer value 0) on failure; always check before using the pointer.
In the context of dynamic memory, what does 'memory fragmentation' refer to?
Fragmentation occurs when memory is allocated and freed irregularly, leaving unused gaps that waste space.
Advertisement
What will happen if you try to allocate extremely large memory?
int *p = malloc(INT_MAX);
malloc() returns NULL if allocation fails (insufficient memory); always check the return value.
Which best practice prevents memory leaks in complex programs?
Disciplined memory management with matching alloc/free pairs and proper error handling prevents leaks.
What does realloc() do if the new size is smaller than the old size?
realloc() shrinks or expands memory. If new size is smaller, it reduces allocation and returns pointer to resized block.
What will be the output of this code?
int *p = (int*)malloc(5 * sizeof(int));
printf("%d", sizeof(p));
sizeof(p) returns the size of pointer itself (4 bytes on 32-bit, 8 bytes on 64-bit), not the allocated memory.
Which statement about free() is correct?
Double-free causes undefined behavior (segmentation fault). free() doesn't set pointer to NULL; programmer must do it explicitly.
In dynamic 2D array creation:
int arr = (int)malloc(m * sizeof(int*));
what does the first malloc() allocate?
First malloc allocates an array of m pointers. Each pointer must be individually allocated in a loop for actual row data.
What happens if you use a pointer after calling free() on it (without reallocating)?
After free(), accessing the pointer causes undefined behavior as memory is returned to the heap and may be reused.
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.
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.
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.