Tricky and Buggy Questions & Answers on malloc, calloc, free and realloc Calls – 2

This set of Tricky and Buggy questions and answers focuses on malloc, calloc, free and realloc Calls.

1. What is the output of this program?

  1.    #include<stdio.h>
  2.  
  3.    int main()
  4.    {
  5.        int *ptr;
  6.        ptr = (int *)calloc(1,sizeof(int));
  7.        *ptr = 10;
  8.        printf("%d\n",*ptr);
  9.        return 0;
  10.    }

a) 0
b) -1
c) 10
d) none of the mentioned
View Answer

Answer: d
Explanation: This program will give an error because calloc() requires the header file stdlib.h.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
san.c: In function ‘main’:
san.c:6:15: warning: incompatible implicit declaration of built-in function ‘calloc’ [enabled by default] [root@localhost sanfoundry]#

2. What is the output of this program?

advertisement
advertisement
  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.  
  4.    int main()
  5.    {
  6.        int *ptr;
  7.        *ptr = 10;
  8.        *ptr = 20;
  9.        printf("%d\n",*ptr);
  10.        return 0;
  11.    }

a) 10
b) 20
c) segmentation fault
d) none of the mentioned
View Answer

Answer: c
Explanation: The segmentation fault occurs because memory for the pointer has not been allocated in this program.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Segmentation fault (core dumped)
[root@localhost sanfoundry]#
Note: Join free Sanfoundry classes at Telegram or Youtube

3. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.  
  4.    int main()
  5.    {
  6.        int *ptr1, *ptr2;
  7.        ptr1 = malloc(4);
  8.        *ptr1 = 10;
  9.        *ptr2 = free(ptr1);
  10.        printf("%d\n",*ptr2);
  11.        return 0;
  12.    }

a) 10
b) it will print the address stored in ptr1
c) it will print the address stored in ptr2
d) it will give an error
View Answer

Answer: d
Explanation: The free() function returns no value.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
san.c: In function ‘main’:
san.c:8:8: error: void value not ignored as it ought to be
[root@localhost sanfoundry]#
advertisement

4. What is the output of this program?

advertisement
  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.  
  4.    int main()
  5.    { 
  6.        int *ptr1;
  7.        while(1){
  8.            ptr1 = malloc(1024*1024);
  9.            if(ptr1 == 0)
  10.                break;
  11.            sleep(1);
  12.            printf("Sanfoundry\n");
  13.            free(ptr1);
  14.        }		
  15.        return 0;
  16.    }

a) it will print “Sanfoundry” until the process has been stopeed by any signal
b) it will print nothing
c) segmentation fault
d) none of the mentioned
View Answer

Answer: a
Explanation: None.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Sanfoundry
Sanfoundry
Sanfoundry
Sanfoundry
Sanfoundry
^Z
[10]+ Stopped ./san
[root@localhost sanfoundry]#

5. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.  
  4.    int main()
  5.    {
  6.        int ret;
  7.        int *ptr;
  8.        ptr = (int *)malloc(sizeof(int)*10);
  9.        free(ptr);
  10.        free(ptr);
  11.        return 0;
  12.    }

a) it will print nothing
b) it will give segmentaion fault
c) undefined behaviour
d) none of the mentioned
View Answer

Answer: c
Explanation: If the free() has already called before, undefined behaviour occurs.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
*** glibc detected *** ./san: double free or corruption (fasttop): 0x08f1b008 ***
======= Backtrace: =========
/lib/libc.so.6[0x4a6489f2] ./san[0x8048425] /lib/libc.so.6(__libc_start_main+0xf3)[0x4a5e96b3] ./san[0x8048361] ======= Memory map: ========
08048000-08049000 r-xp 00000000 fd:01 394194 /home/sanfoundry/san
08049000-0804a000 rw-p 00000000 fd:01 394194 /home/sanfoundry/san
08f1b000-08f3c000 rw-p 00000000 00:00 0 [heap] 4a5ab000-4a5cc000 r-xp 00000000 fd:01 785334 /lib/ld-2.14.90.so
4a5cc000-4a5cd000 r–p 00020000 fd:01 785334 /lib/ld-2.14.90.so
4a5cd000-4a5ce000 rw-p 00021000 fd:01 785334 /lib/ld-2.14.90.so
4a5d0000-4a77a000 r-xp 00000000 fd:01 789110 /lib/libc-2.14.90.so
4a77a000-4a77b000 —p 001aa000 fd:01 789110 /lib/libc-2.14.90.so
4a77b000-4a77d000 r–p 001aa000 fd:01 789110 /lib/libc-2.14.90.so
4a77d000-4a77e000 rw-p 001ac000 fd:01 789110 /lib/libc-2.14.90.so
4a77e000-4a781000 rw-p 00000000 00:00 0
4a7e0000-4a7fc000 r-xp 00000000 fd:01 789128 /lib/libgcc_s-4.6.2-20111027.so.1
4a7fc000-4a7fd000 rw-p 0001b000 fd:01 789128 /lib/libgcc_s-4.6.2-20111027.so.1
b76f4000-b76f5000 rw-p 00000000 00:00 0
b770d000-b770f000 rw-p 00000000 00:00 0
b770f000-b7710000 r-xp 00000000 00:00 0 [vdso] bfc0a000-bfc2b000 rw-p 00000000 00:00 0 [stack] Aborted (core dumped)
[root@localhost sanfoundry]#

6. In which condition this prgram will print the string “Sanfoundry”?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.  
  4.    int main()
  5.    {
  6.        int *ptr;
  7.        ptr = (int *)malloc(sizeof(int)*10);
  8.        if (ptr == NULL)
  9.            printf("Sanfoundry\n");
  10.        return 0;
  11.    }

a) if the memory could not be allocated to the pointer “ptr”
b) if the memory has been allocated to the pointer “ptr” successfully
c) it will never print
d) none of the mentioned
View Answer

Answer: a
Explanation: The malloc() returns NULL when the memory is not allocated.

7. This program will allocate the memory of ___ bytes for pointer “ptr”.

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.  
  4.    int main()
  5.    {
  6.        int *ptr;
  7.        ptr = (int*)malloc(sizeof(int)*4);
  8.        ptr = realloc(ptr,sizeof(int)*2);
  9.        return 0;
  10.    }

a) 2
b) 4
c) 8
d) none of the mentioned
View Answer

Answer: c
Explanation: We can also use the realloc() to make memory block smaller.

8. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.  
  4.    int main()
  5.    {
  6.        int *ptr;
  7.        ptr = (int *)calloc(1,sizeof(int));
  8.        if (ptr != 0)
  9.            printf("%d\n",*ptr);
  10.        return 0;
  11.    }

a) 0
b) -1
c) garbage value
d) none of the mentioned
View Answer

Answer: a
Explanation: The memory allocated by calloc() contains 0 until process does not make any change to it.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
0
[root@localhost sanfoundry]

9. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.  
  4.    int main()
  5.    {
  6.        int *ptr;
  7.        ptr = (int *)malloc(sizeof(int));
  8.        printf("%d\n",*ptr);
  9.        return 0;
  10.    }

a) 4
b) -1
c) undefined
d) none of the mentioned
View Answer

Answer: c
Explanation: The content of the memory block allocated by malloc() is undefined.
Ouput:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
0
[root@localhost sanfoundry]#

10. In this program the allocated memory block can store

  1.     #include<stdio.h>
  2.     #include<stdlib.h>
  3.  
  4.     int main()
  5.     {
  6.         int *ptr;
  7.         ptr = malloc(10);
  8.         return 0;
  9.     }

a) int
b) char
c) float
d) all of the mentioned
View Answer

Answer: d
Explanation: When the malloc() is used without typecasting the default type is void*.

Sanfoundry Global Education & Learning Series – Linux Administration & Programming.
Here’s the list of Best Books in Linux Commands & Shell Programming.
Here’s the list of Best Books in Linux Kernel, Device-Drivers & System Programming.

To practice all questions on Linux Administration & Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on Linux.

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.