C Memory Allocation Questions
2. What will be the output of the C program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i, numbers[1];
numbers[0] = 9;
free(numbers);
printf("\nStored integers are ");
printf("\nnumbers[%d] = %d ", 0, numbers[0]);
return 0;
}
A. 9
B. 0
C. Compilation error
D. garbage value
3. What will be the output of the C program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *j = (int*)malloc(4 * sizeof(int));
*j = 9;
free(j);
printf("%d", *j);
return 0;
}
A. Compilation error
B. 0
C. Some Garbage value
D. Nothing prints
4. What will be the output of the C program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *numbers = (int*)calloc(4, sizeof(int));
numbers[0] = 9;
free(numbers);
printf("\nStored integers are ");
printf("\nnumbers[%d] = %d ", 0, numbers[0]);
return 0;
}
A. Garbage value
B. 0
C. 9
D. Compilation error
5. What will be the output of the C program?
#include<stdio.h>
int main()
{
int *ptr = (int *)malloc(sizeof(int));
*ptr = 10;
free(ptr);
p = 9;
printf("%d", ptr);
}
A. Compilation error
B. 0
C. 9
D. Garbage value
6. What is the return type of malloc() or calloc()?
A. int *
B. int **
C. void *
D. void **
7. What will be the output of the C program?
#include<stdio.h>
#include<string.h>
int main()
{
int i;
char *ptr;
char *fun();
ptr = fun();
printf(" %s", ptr);
return 0;
}
char *fun()
{
char disk[30];
strcpy(disk, "memory in c");
printf("%s ",disk);
return disk;
}
A. memory in c
B. memory in c memory in c
C. Compilation error
D. garbage value
8. What will be the output of the C program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p;
p = (int *)malloc(20);
printf("%d\n", sizeof(p));
free(p);
return 0;
}
A. 40
B. 20
C. 4
D. 80
10. What will be the output of the C program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
struct test
{
int i;
float f;
char c;
};
struct test *ptr;
ptr = (struct test *)malloc(sizeof(struct test));
ptr ->f = 6.5f;
printf("%f", ptr->f);
return 0;
}
A. Compilation error
B. Garbage value
C. 6.500000
D. 0.000000
Consider the following three C functions :
[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
A
Only P1 and P3
B
Only P1 and P2
C
P1, P2 and P3
D
Question 7
What is the problem with following code?
#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
A
Memory Leak
B
Dangling Pointer
C
The program may crash as free() is called for NULL pointer
D
Question 8
Consider the following program, where are i,
j and k are stored in memory?
int i;
int main()
{
int j;
int *k = (int *) malloc (sizeof(int));
}
Run on IDE
i, j and *k are stored in stack segment
A
i and j are stored in stack segment. *k is stored on heap.
B
i is stored in BSS part of data segment, j is stored in stack segment. *k is stored on
C
j is stored in BSS part of data segment, i is stored in stack segment. *k is stored on
D
Question 1
The most appropriate matching for the following pairs (GATE CS 2000)
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
A
(X—2 Y—1 Z-3
B
X—3 Y—2 Z-1
C
X—3 Y—1 Z-2
D
Point out the error in the following program.
#include <stdio.h>
#include <stdlib.h>
int main()
char *ptr;
*ptr = (char)malloc(30);
strcpy(ptr, "RAM");
printf("%s", ptr);
free(ptr);
return 0;
A. Error: in strcpy() statement
B. Error: in *ptr = (char)malloc(30);
C. Error: in free(ptr);
D. No Error
If malloc() successfully allocates memory it returns the number of
bytes it has allocated.
# include<stdio.h>
#include<stdlib.h>
void fun(int *a)
a = (int*)malloc(sizeof(int));
int main()
int *p;
fun(p);
*p = 6;
printf("%dn",*p);
return(0);
A. May not work
B. Works and prints 6
C. Compiler Error
D. Runtime error
Which statment is true about the given code ?
#include <stdio.h>
#include <stdlib.h>
int main()
int *a[5];
a = (int*) malloc(sizeof(int)*5);
free(a);
return 0;
A. Error: unable to allocate memory
B. Error: We cannot store address of allocated memory in a
C. Error: unable to free memory
D. No error
20. How will you free the memory allocated by the following program?
#include <stdio.h>
#include <stdlib.h>
#define MAXROW 2
#define MAXCOL 3
int main()
int **p, i, j;
p = (int **) malloc(MAXROW * sizeof(int*));
return 0;
}
A. memfree(int p);
B. dealloc(p);
C. malloc(p, 0);
D. free(p);