What is the purpose of typecasting in malloc()? int *p = (int*)malloc(sizeof(int));
A It is mandatory in C B It improves memory allocation speed C It improves code readability and is good practice (optional in C, mandatory in C++) D It prevents memory leaks
Typecasting malloc() is optional in C but improves readability. It's mandatory in C++.
Analyze the memory leak in this code: void func() { int *p = malloc(100); if(condition) return; free(p); }
A No memory leak; free() is called before return B Memory leak occurs when condition is true (p is not freed) C Memory leak occurs in all cases D No leak, malloc() automatically frees memory
When condition is true, the function returns without calling free(), causing a memory leak.
What happens when you call free() on a NULL pointer?
A Program crashes B It is safely ignored (no operation performed) C It returns false D Undefined behavior occurs
According to C standard, free(NULL) is safe and does nothing.
Which statement best describes dynamic memory allocation's advantage over static allocation?
A Dynamic memory is always faster B Size is determined at compile time with dynamic allocation C Memory size can be determined at runtime based on actual needs D Dynamic memory never causes fragmentation
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]);
A Random values B 0 0 0 C Garbage, Garbage, Garbage D Compilation error
calloc() initializes all allocated bytes to 0, so all integers are 0.
Consider reallocating memory: int *p = malloc(10); p = realloc(p, 20);
A The original 10 bytes are lost B realloc() preserves existing data and allocates additional memory C realloc() always returns a new address D Memory at address p is deallocated first
realloc() extends the existing allocation, preserving original data, and may return the same or different address.
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
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?
A Static array allocation B malloc() for each node as needed C calloc() all nodes upfront D Stack allocation
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");
A Memory leak (str not freed) B Buffer overflow - writing 11 characters into 5 bytes C strcpy() is deprecated D Both A and B
Buffer overflow occurs (11 chars into 5 bytes), and memory is never freed, causing a leak.
How can you safely check if malloc() succeeded?
A Check if pointer equals 0 or NULL B Dereference the pointer immediately C Use sizeof() on the pointer D Compare with void pointer
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?
A Breaking memory into chunks that cannot be used efficiently B Allocating too much memory at once C Using calloc() instead of malloc() D Accessing freed memory
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);
A It will always succeed B It will return NULL on failure, and you should check for this C It will crash immediately D Compiler error
malloc() returns NULL if allocation fails (insufficient memory); always check the return value.
Which best practice prevents memory leaks in complex programs?
A Allocate all memory at program start B Never use dynamic allocation C Track allocations and ensure every malloc() has a corresponding free() D Use realloc() instead of free()
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; };
A struct Node *n = malloc(sizeof(struct Node)); n->data = 5; B struct Node *n = (struct Node*)malloc(sizeof(struct Node)); n->data = 5; C struct Node n = malloc(sizeof(struct Node)); D Both A and B are equally correct
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?
A #include <stdlib.h> B #include <stdio.h> C #include <string.h> D #include <memory.h>
stdlib.h contains malloc(), calloc(), realloc(), and free() functions for dynamic memory management.
What is the return type of malloc()?
A void* B int* C char* D NULL
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?
A Increases the memory block B Shrinks the memory block and returns a pointer C Frees extra memory automatically D Returns NULL
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));
A 20 B 4 (or 8 on 64-bit systems) C 5 D Undefined
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?
A free() must always be called exactly once per malloc() B free() sets the pointer to NULL automatically C Calling free() twice causes undefined behavior D free() returns 1 on success, 0 on failure
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?
A m rows of integers B An array of m pointers to int C m*n integers D Memory for entire 2D array at once
First malloc allocates an array of m pointers. Each pointer must be individually allocated in a loop for actual row data.