What is the purpose of typecasting in malloc()?
int *p = (int*)malloc(sizeof(int));
Analyze the memory leak in this code:
void func() {
int *p = malloc(100);
if(condition) return;
free(p);
}
What happens when you call free() on a NULL pointer?
Which statement best describes dynamic memory allocation's advantage over static allocation?
What is the output of this code?
int *p = calloc(3, sizeof(int));
printf("%d %d %d", p[0], p[1], p[2]);
Advertisement
Consider reallocating memory:
int *p = malloc(10);
p = realloc(p, 20);
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);
}
Which allocation method is most suitable for a dynamically growing linked list?
What is the risk in this code?
char *str = malloc(5);
strcpy(str, "Hello World");
How can you safely check if malloc() succeeded?
In the context of dynamic memory, what does 'memory fragmentation' refer to?
What will happen if you try to allocate extremely large memory?
int *p = malloc(INT_MAX);
Which best practice prevents memory leaks in complex programs?
What is the correct way to allocate and initialize a structure dynamically?
struct Node { int data; int next; };
Which header file must be included for dynamic memory allocation functions in C?
What is the return type of malloc()?
What does realloc() do if the new size is smaller than the old size?
What will be the output of this code?
int *p = (int*)malloc(5 * sizeof(int));
printf("%d", sizeof(p));
Which statement about free() is correct?
In dynamic 2D array creation:
int arr = (int)malloc(m * sizeof(int*));
what does the first malloc() allocate?