Analyze the memory leak in this code:
void func() {
int *p = malloc(100);
if(condition) return;
free(p);
}
When condition is true, the function returns without calling free(), causing a memory leak.
What happens when you call free() on a NULL pointer?
According to C standard, free(NULL) is safe and does nothing.
Which statement best describes dynamic memory allocation's advantage over static allocation?
Dynamic memory allows flexible sizing at runtime, which static arrays cannot provide.
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.
Advertisement
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);
}
Memory is freed in func2(), then again in func(), causing double free error.
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.
What is the risk in this code?
char *str = malloc(5);
strcpy(str, "Hello World");
Buffer overflow occurs (11 chars into 5 bytes), and memory is never freed, causing a leak.
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.
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 is the correct way to allocate and initialize a structure dynamically?
struct Node { int data; int next; };
Both A and B allocate memory correctly; B includes explicit typecasting which is optional in C but good practice.
Which header file must be included for dynamic memory allocation functions in C?
stdlib.h contains malloc(), calloc(), realloc(), and free() functions for dynamic memory management.
What is the return type of malloc()?
malloc() returns a generic pointer (void*) which can be cast to any data type pointer.
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.