Home Subjects C Programming Dynamic Memory

C Programming
Dynamic Memory

C language from basics to advanced placement prep

100 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 61–70 of 100
Topics in C Programming
Q.61 Medium Dynamic Memory
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
Correct Answer:  B. 4 (or 8 on 64-bit systems)
EXPLANATION

sizeof(p) returns the size of pointer itself (4 bytes on 32-bit, 8 bytes on 64-bit), not the allocated memory.

Take Test
Q.62 Medium Dynamic Memory
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
Correct Answer:  B. Shrinks the memory block and returns a pointer
EXPLANATION

realloc() shrinks or expands memory. If new size is smaller, it reduces allocation and returns pointer to resized block.

Take Test
Q.63 Easy Dynamic Memory
What is the return type of malloc()?
A void*
B int*
C char*
D NULL
Correct Answer:  A. void*
EXPLANATION

malloc() returns a generic pointer (void*) which can be cast to any data type pointer.

Take Test
Q.64 Easy Dynamic Memory
Which header file must be included for dynamic memory allocation functions in C?
A #include
B #include
C #include
D #include
Correct Answer:  A. #include
EXPLANATION

stdlib.h contains malloc(), calloc(), realloc(), and free() functions for dynamic memory management.

Take Test
Q.65 Easy Dynamic Memory
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
Correct Answer:  D. Both A and B are equally correct
EXPLANATION

Both A and B allocate memory correctly; B includes explicit typecasting which is optional in C but good practice.

Take Test
Q.66 Medium Dynamic Memory
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()
Correct Answer:  C. Track allocations and ensure every malloc() has a corresponding free()
EXPLANATION

Disciplined memory management with matching alloc/free pairs and proper error handling prevents leaks.

Take Test
Q.67 Medium Dynamic Memory
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
Correct Answer:  B. It will return NULL on failure, and you should check for this
EXPLANATION

malloc() returns NULL if allocation fails (insufficient memory); always check the return value.

Take Test
Q.68 Medium Dynamic Memory
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
Correct Answer:  A. Breaking memory into chunks that cannot be used efficiently
EXPLANATION

Fragmentation occurs when memory is allocated and freed irregularly, leaving unused gaps that waste space.

Take Test
Q.69 Medium Dynamic Memory
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
Correct Answer:  A. Check if pointer equals 0 or NULL
EXPLANATION

malloc() returns NULL (pointer value 0) on failure; always check before using the pointer.

Take Test
Q.70 Hard Dynamic Memory
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
Correct Answer:  D. Both A and B
EXPLANATION

Buffer overflow occurs (11 chars into 5 bytes), and memory is never freed, causing a leak.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips