C Questions and Answers – DMA Functions, Memory Leak, Dangling Pointers – 1

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “DMA Functions, Memory Leak, Dangling Pointers – 1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

1. What will be the output of the following C code if the input entered as first and second number is 5 and 6 respectively?

#include<stdio.h>
#include<stdlib.h>
main()
{
    int *p;
    p=(int*)calloc(3*sizeof(int));
    printf("Enter first number\n");
    scanf("%d",p);
    printf("Enter second number\n");
    scanf("%d",p+2);
    printf("%d%d",*p,*(p+2));
    free(p);
}

a) 56
b) Address of the locations where the two numbers are stored
c) 57
d) Error
View Answer

Answer: d
Explanation: The above code results in an error. This is because the syntax of the function calloc() is incorrect. In order to rectify this error, we must write: calloc(3,sizeof(int));
advertisement
advertisement

2. In the function malloc(), each byte of allocated space is initialized to zero.
a) True
b) False
View Answer

Answer: b
Explanation: In the function malloc(), allocated space is initialized to junk values. In calloc(), each byte of allocated space is initialized to zero.
Note: Join free Sanfoundry classes at Telegram or Youtube

3. Which of the following functions allocates multiple blocks of memory, each block of the same size?
a) malloc()
b) realloc()
c) calloc()
d) free()
View Answer

Answer: c
Explanation: malloc() allocates a single block of memory whereas calloc() allocates multiple blocks of memory, each block with the same size.

4. A condition where in memory is reserved dynamically but not accessible to any of the programs is called _____________
a) Memory leak
b) Dangling pointer
c) Frozen memory
d) Pointer leak
View Answer

Answer: a
Explanation: If we allocate memory dynamically in a function (malloc, calloc, realloc), the allocated memory will not be de-allocated automatically when the control comes out of the function. This allocated memory cannot be accessed and hence cannot be used. This unused inaccessible memory results in a memory leak.
advertisement

5. What will happens if the statement free(a) is removed in the following C code?

#include<stdio.h>
#include<stdlib.h>
main()
{
    int *a;
    a=(int*)malloc(sizeof(int));
    *a=100;
    printf("*a%d",*a);
    free(a);
    a=(int*)malloc(sizeof(int));
    *a=200;
    printf("a%p",a);
    *a=200;
    printf("a%d",*a);
}

a) Error
b) Memory leak
c) Dangling pointer arises
d) 200 is printed as output
View Answer

Answer: b
Explanation: The pointer ‘a’ points to the recent value 200, making the memory allocated earlier inaccessible. Hence, the memory where the value 100 is inaccessible and cannot be freed. Therefore, memory leak occurs.
advertisement

6. The incorrect statement with respect to dangling pointers is ___________
a) Pointer pointing to non-existent memory location is called dangling pointer
b) When a dynamically allocated pointer references the original memory after it has been freed, a dangling pointer arises
c) If memory leak occurs, it is mandatory that a dangling pointer arises
d) Dangling pointer may result in segmentation faults and potential security risks
View Answer

Answer: c
Explanation: Memory leak and dangling pointers are not inter dependent. Hence, when memory leak occurs, it is not mandatory that a dangling pointer arises

7. What will be the output of the following C code?

#include<stdio.h>
#include<stdlib.h>
void main()
{
    char *p = calloc(100, 1);
    p = "welcome";
    printf("%s\n", p);
}

a) error
b) welcome
c) memory location stored by the pointer
d) junk value
View Answer

Answer: b
Explanation: There is no error in the above code. The format specifier being %s, address is not returned. Hence, welcome is the output.

8. In the function realloc(), if the new size of the memory block is larger than the old size, then the added memory ___________
a) is initialized to junk values
b) is initialized to zero
c) results in an error
d) is not initialized
View Answer

Answer: d
Explanation: The function realloc() changes the size of a particular memory block. If the new size is larger than the old size, the added memory is not initialized.

9. The free() function frees the memory state pointed to by a pointer and returns ___________
a) the same pointer
b) the memory address
c) no value
d) an integer value
View Answer

Answer: c
Explanation: The free() function frees the memory state pointed by a pointer and returns no value.

10. The following C code is an example of __________

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
    char *p,*q;
    p=(char*)malloc(3*sizeof(char));
    q=p;
    strcpy(p,"hello");
    printf("p=%s",p);
    printf("q=%s",q);
    free(q);
    q=NULL;
    gets(p);
    gets(q);
    printf("%s",p);
    printf(%s”,q);
}

a) Memory leak
b) Dangling pointer
c) Static memory allocation
d) Linked list
View Answer

Answer: b
Explanation: In the above code, the pointer p, points to a memory location which has been freed. Hece the above code is an example of dangling pointer.

Sanfoundry Global Education & Learning Series – C Programming Language.

To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.