C Objective Questions on “DMA Functions, Memory Leak, Dangling Pointers – 2”.
1. What will be the output of the following C code if it is executed on a 32 bit processor?
#include#include int main() { int *p; p = (int *)malloc(20); printf("%dn", sizeof(p)); free(p); return 0; }
a) 2
b) 4
c) 8
d) Junk value
Answer: b
Clarification: The size of a pointer is 2 bytes on a 16 bit platform, 4 bytes on a 32 bit platform and 8 bytes on a 64 bit platform.
2. The number of arguments taken as input which allocating memory dynamically using malloc() is ___________
a) 0
b) 1
c) 2
d) 3
Answer: b
Clarification: An example of memory allocated using malloc():
(int*)malloc(3*sizeof(int)
It is clear from the above example that malloc() takes only one argument as input, that is the number of bytes to be allocated.
3. Suppose we have a one dimensional array, named ‘x’, which contains 10 integers. Which of the following is the correct way to allocate memory dynamically to the array ‘x’ using malloc()?
a) x=(int*)malloc(10);
b) x=(int*)malloc(10,sizeof(int));
c) x=malloc(int 10,sizeof(int));
d) x=(int*)malloc(10*sizeof(int));
Answer: d
Clarification: According to the syntax of malloc, the correct way to do the specified operation is: x=(int*)malloc(10*sizeof(int)); This operation can also be performed using calloc(). In that case, the method will be: x=(int*)calloc(10,sizeof(int));
4. What will be the error (if any) in the following C code?
#include#include #include int main() { char *p; *p = (char)calloc(10); strcpy(p, "HELLO"); printf("%s", p); free(p); return 0; }
a) No error
b) Error in the statement: strcpy(p,”HELLO”);
c) Error in the statement: *p=(char)calloc(10);
d) Error in the statement: free(p);
Answer: c
Clarification: The syntax for dynamically allocating memory using calloc() is incorrect. Hence, this code results in an error. The correct syntax for calloc is: void*calloc(size_t n, size_t size);
5. If malloc() and calloc() are not type casted, the default return type is ___________
a) void*
b) void**
c) int*
d) char*
Answer: a
Clarification: If malloc() and calloc() are not type casted, they return a pointer of the type void.
6. Pick out the correct statement with respect to the heap.
a) Local variables are stored on the heap
b) Static variables are stored on the heap
c) Heap is the data structure which is used to implement recursive function calls
d) Everything on the heap is anonymous
Answer: a
Clarification: Local variables are stored on the stack. Static variables are stored in the permanent storage area. Stack is the data structure used to implement recursive function calls. Hence, it is true that everything on heap s anonymous.
7. What will be the output of the following C code? (Given that the size of array is 4 and new size of array is 5)
#include#include main() { int *p,i,a,b; printf("Enter size of array"); scanf("%d",&a); p=(int*)malloc(a*sizeof(int)); for(i=0;i<a;i++) printf("%dn",i); printf("Enter new size of array"); scanf("%d",&b); realloc(p,b); for(i=0;i<b;i++) printf("%dn",i); free(p); }
a)
1234 12345
b) Error
c)
0123 01234
d)
0123 12345
View Answer
Answer: c
Clarification: In the above code, we are reallocating memory. When the size of the array is 4, the output is 0123. When the size of the array is changed to 5, the output is 01234. Hence, the output is:
0123
01234
8. When the pointer is NULL, then the function realloc is equivalent to the function ___________
a) malloc
b) calloc
c) free
d) alloc
Answer: a
Clarification: If pointer is NULL, the call to the function realloc is equal to malloc(size), for any value of size. If size is equal to zero, then the pointer is not NULL and the call is equivalent to free(pointer).
9. Garbage collector frees the programmer from worrying about ___________
a) Dangling pointers
b) Creating new objects
c) Memory leak
d) Segmentation errors
Answer: c
Clarification: A garbage collector is a program that automatically removes unwanted data held temporarily in the memory during processing. Hence it frees the programmer from worrying about memory leaks.
10. If the space in memory allocated by malloc is not sufficient, then an allocation fails and returns ___________
a) NULL pointer
b) Zero
c) Garbage value
d) The number of bytes available
Answer: a
Clarification: A NULL pointer is returned when the memory allocated by malloc dynamically is insufficient.