What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int ret;
int *ptr;
ptr = (int *)malloc(sizeof(int)*10);
free(ptr);
free(ptr);
return 0;
}
a) it will print nothing
b) it will give segmentaion fault
c) undefined behaviour
d) none of the mentioned
Submitted by: Murtazac) undefined behaviour
Explanation:
If the free() has already called before, undefined behaviour occurs.
Output:
[root@localhost google]# gcc -o san san.c
[root@localhost google]# ./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/google/san
08049000-0804a000 rw-p 00000000 fd:01 394194 /home/google/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 google]#
Submitted by: Murtaza
Explanation:
If the free() has already called before, undefined behaviour occurs.
Output:
[root@localhost google]# gcc -o san san.c
[root@localhost google]# ./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/google/san
08049000-0804a000 rw-p 00000000 fd:01 394194 /home/google/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 google]#
Submitted by: Murtaza
Read Online Linux Startup and Shutdown Job Interview Questions And Answers
Top Linux Startup and Shutdown Questions
☺ | At the end of kernel bootstrap, which process is started? |
☺ | Single user mode shell runs as: |
☺ | Which is the only partition mounted in Single user mode? |
☺ | Which of the following is not a valid run-level? |
☺ | The shell used for Single user mode shell is: |
Top Linux OS Categories
☺ | Device Drivers Interview Questions. |
☺ | Linux OS Management Interview Questions. |
☺ | Linux Makefile Interview Questions. |
☺ | Linux Environment Interview Questions. |
☺ | Linux OS Shell Interview Questions. |