C Dynamic Memory Allocation
Question 1 |
X: m=malloc(5); m= NULL; 1: using dangling pointers Y: free(n); n->value=5; 2: using uninitialized pointers Z: char *p; *p = ’a’; 3. lost memory is:
X—1 Y—3 Z-2 | |
(X—2 Y—1 Z-3 | |
X—3 Y—2 Z-1 | |
X—3 Y—1 Z-2 |
Discuss it
Question 2 |
[PI] int * g (void) { int x= 10; return (&x); } [P2] int * g (void) { int * px; *px= 10; return px; } [P3] int *g (void) { int *px; px = (int *) malloc (sizeof(int)); *px= 10; return px; }Which of the above three functions are likely to cause problems with pointers? (GATE 2001)
Only P3 | |
Only P1 and P3 | |
Only P1 and P2 | |
P1, P2 and P3 |
Discuss it
Question 3 |
Output?
C
# include<stdio.h> # include<stdlib.h> void fun(int *a) { a = (int*)malloc(sizeof(int)); } int main() { int *p; fun(p); *p = 6; printf("%d",*p); return(0); }
May not work | |
Works and prints 6 |
Discuss it
The program is not valid. Try replacing “int *p;” with “int *p = NULL;” and it will try to dereference a null pointer. This is because fun() makes a copy of the pointer, so when malloc() is called, it is setting the copied pointer to the memory location, not p. p is pointing to random memory before and after the call to fun(), and when you dereference it, it will crash. If you want to add memory to a pointer from a function, you need to pass the address of the pointer (ie. double pointer).
Question 4 |
calloc() allocates the memory and also initializes the allocates memory to zero, while memory allocated using malloc() has random data. | |
malloc() and memset() can be used to get the same effect as calloc(). | |
calloc() takes two arguments, but malloc takes only 1 argument. | |
Both malloc() and calloc() return 'void *' pointer. | |
All of the above |
Discuss it
Question 5 |
void * | |
Pointer of allocated memory type | |
void ** | |
int * |
Discuss it
Question 6 |
"ptr = calloc(m, n)" is equivalent to following ptr = malloc(m * n); | |
"ptr = calloc(m, n)" is equivalent to following ptr = malloc(m * n); memset(ptr, 0, m * n); | |
"ptr = calloc(m, n)" is equivalent to following ptr = malloc(m); memset(ptr, 0, m); | |
"ptr = calloc(m, n)" is equivalent to following ptr = malloc(n); memset(ptr, 0, n); |
Discuss it
Question 7 |
#include<stdio.h> int main() { int *p = (int *)malloc(sizeof(int)); p = NULL; free(p); }
Compiler Error: free can't be applied on NULL pointer | |
Memory Leak | |
Dangling Pointer | |
The program may crash as free() is called for NULL pointer. |
Discuss it
free(p); p = NULL;
Question 8 |
int i; int main() { int j; int *k = (int *) malloc (sizeof(int)); }
i, j and *k are stored in stack segment | |
i and j are stored in stack segment. *k is stored on heap. | |
i is stored in BSS part of data segment, j is stored in stack segment. *k is stored on heap. | |
j is stored in BSS part of data segment, i is stored in stack segment. *k is stored on heap. |
Discuss it
Question 9 |
Which languages necessarily need heap allocation in the run time environment?
Those that support recursion | |
Those that use dynamic scoping | |
Those that use global variables | |
Those that allow dynamic data structures |
Discuss it
Languages that allow dynamic data structures necessarily need heap allocation in the runtime environment. This is because dynamic data structures such as arrays, linked lists, trees, and graphs require memory allocation that persists beyond the lifetime of the function in which they are created. Heap allocation allows for the dynamic creation and destruction of memory blocks at runtime, which is essential for implementing dynamic data structures.
Question 10 |
Dynamic memory allocation | |
Static memory allocation | |
Both dynamic and static memory allocation | |
None of the above |
Discuss it