Home Subjects C Programming Dynamic Memory

C Programming
Dynamic Memory

C language from basics to advanced placement prep

53 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 41–50 of 53
Topics in C Programming
Q.41 Medium Dynamic Memory
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
Correct Answer:  B. realloc() preserves existing data and allocates additional memory
EXPLANATION

realloc() extends the existing allocation, preserving original data, and may return the same or different address.

Test
Q.42 Medium Dynamic Memory
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
Correct Answer:  B. 0 0 0
EXPLANATION

calloc() initializes all allocated bytes to 0, so all integers are 0.

Test
Q.43 Medium Dynamic Memory
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
Correct Answer:  B. Memory leak occurs when condition is true (p is not freed)
EXPLANATION

When condition is true, the function returns without calling free(), causing a memory leak.

Test
Q.44 Medium Dynamic Memory
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
Correct Answer:  C. It improves code readability and is good practice (optional in C, mandatory in C++)
EXPLANATION

Typecasting malloc() is optional in C but improves readability. It's mandatory in C++.

Test
Q.45 Medium Dynamic Memory
Which of the following correctly allocates memory for an array of 5 strings (each max 20 characters)?
A char *str = malloc(5 * 20);
B char **str = malloc(5 * sizeof(char*)); for(int i=0; i
C char str[5][20];
D Both B and C are valid approaches
Correct Answer:  D. Both B and C are valid approaches
EXPLANATION

Option B uses dynamic allocation (2D array), Option C uses static allocation. Both are valid but for different scenarios.

Test
Q.46 Medium Dynamic Memory
What is the output?
int *p = malloc(sizeof(int) * 3);
p[0] = 1; p[1] = 2; p[2] = 3;
printf("%d", *(p+2));
A 1
B 2
C 3
D Compilation error
Correct Answer:  C. 3
EXPLANATION

*(p+2) is equivalent to p[2], which contains 3. Pointer arithmetic works with dynamic arrays.

Test
Q.47 Medium Dynamic Memory
Analyze this code:
char *str = malloc(5);
strcpy(str, "Hello");
How many bytes should malloc allocate for safety?
A 5
B 6
C 4
D 7
Correct Answer:  B. 6
EXPLANATION

String "Hello" has 5 characters plus 1 null terminator (\0), requiring 6 bytes total.

Test
Q.48 Medium Dynamic Memory
What is double free error?
A Allocating memory twice
B Calling free() on already freed memory
C Using two pointers to same memory
D Allocating memory twice and freeing once
Correct Answer:  B. Calling free() on already freed memory
EXPLANATION

Double free error occurs when free() is called on the same memory block twice, causing undefined behavior and program crash.

Test
Q.49 Medium Dynamic Memory
Which of the following is correct for dynamic array allocation?
A int arr[n]; where n is variable
B int *arr = malloc(n * sizeof(int));
C int arr = malloc(n);
D int *arr[n];
Correct Answer:  B. int *arr = malloc(n * sizeof(int));
EXPLANATION

Dynamic array allocation requires pointer and malloc() with proper size calculation. VLAs are not standard C.

Test
Q.50 Medium Dynamic Memory
What is the purpose of realloc() function?
A To allocate new memory
B To resize previously allocated memory block
C To free memory
D To initialize memory
Correct Answer:  B. To resize previously allocated memory block
EXPLANATION

realloc() resizes a previously allocated memory block. It can increase or decrease the size and preserves existing data.

Test
IGET
IGET AI
Online · Exam prep assistant
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