Linux Debugging Questions & Answers on malloc, calloc, free and realloc Calls…

This set of Linux Debugging questions and answers focuses on malloc, calloc, free and realloc Calls.

1. 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 = realloc(0,sizeof(int)*10);
  8.        return 0;
  9.    }

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

Answer: c
Explanation: If the first argument of realloc() is NULL, then it behaves just like malloc().

2. What is the output of this program?

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

a) this program will print nothing after execution
b) segmentation fault
c) Aborted (core dumped)
d) none of the mentioned
View Answer

Answer: c
Explanation: This prgram is trying to free the memory which is not available in the heap segment.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
*** glibc detected *** ./san: free(): invalid pointer: 0x4a77cff4 ***
======= Backtrace: =========
/lib/libc.so.6[0x4a6489f2] ./san[0x80483c9] /lib/libc.so.6(__libc_start_main+0xf3)[0x4a5e96b3] ./san[0x8048321] ======= Memory map: ========
08048000-08049000 r-xp 00000000 fd:01 394194 /home/sanfoundry/san
08049000-0804a000 rw-p 00000000 fd:01 394194 /home/sanfoundry/san
09233000-09254000 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
b7724000-b7725000 rw-p 00000000 00:00 0
b773d000-b773f000 rw-p 00000000 00:00 0
b773f000-b7740000 r-xp 00000000 00:00 0 [vdso] bfc83000-bfca4000 rw-p 00000000 00:00 0 [stack] Aborted (core dumped)
[root@localhost sanfoundry]#

3. What is the output of this program?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    struct st{
  4.        int a;
  5.        char b;
  6.    };
  7.  
  8.    int main()
  9.    {
  10.        struct st *st_ptr;
  11.        st_ptr = malloc(sizeof(struct st));
  12.        printf("%d\n",sizeof(struct st));
  13.        return 0;
  14.    }

a) 8
b) 5
c) 0
d) none of the mentioned
View Answer

Answer: a
Explanation: Maximum size of the data type is 4 byte(int) in the structure.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
8
[root@localhost sanfoundry]#

4. What is the output of this program?

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

a) sanfoundry
b) segmentation fault
c) syntax error
d) none of the mentioned
View Answer

Answer: b
Explanation: value of a string can not be assign to a pointer.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Segmentation fault (core dumped)
[root@localhost sanfoundry]#

5. What is the output of this program?

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

a) sanfoudry
b) segmentation fault
c) syntax error
d) none of the mentioned
View Answer

Answer: b
Explanation: Memory must be allocated to pointer “ptr”.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Segmentation fault (core dumped)
[root@localhost sanfoundry]#

6. What is the output of this program?

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

a) s
b) sanfoundry
c) 115
d) segmentation fault
View Answer

Answer: c
Explanation: This program will print the equivalent decimal value at location pointed by “ptr”.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
115
[root@localhost sanfoundry]#

7. Which one of the following in true about this program?

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

a) this program will give segmentation fault
b) this program will print two same values
c) this program has some syntax error
d) none of the mentioned
View Answer

Answer: d
Explanation: This program will print two different values.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
0x4a77cff4
0x980c008
[root@localhost sanfoundry]#

8. In this program the two printed memory locations has the difference of ___ bytes.

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

a) 1
b) 4
c) can not be determined
d) none of the mentioned
View Answer

Answer: b
Explanation: Pointer will increment by 4 bytes because it is the types of integer.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
0x9b4e008
0x9b4e00c
[root@localhost sanfoundry]#

9. What is the output of this program?

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

a) 4
b) 8
c) the compiler will give the error
d) segmentaion fault
View Answer

Answer: c
Explanation: Just see the output carefully.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
san.c: In function ‘main’:
san.c:8:10: error: conflicting types for ‘ptr’
san.c:7:7: note: previous declaration of ‘ptr’ was here
[root@localhost sanfoundry]#

10. What is the output of this program?

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

a) syntax error
b) segmentaion fault
c) run time error
d) none of the mentioned
View Answer

Answer: d
Explanation: The memory has been allocated but we can not access rest of the memory other than 4 bytes.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
[root@localhost sanfoundry]#

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.

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.