Wednesday, September 18, 2013

segmentation fault with printf on linux

segmentation fault with printf on linux

@Team,
While trying to print an integer value through printf i accidentally wrote
the statement as
int x =10;
printf(x);
In linux i am getting a segmentation fault when tried to execute it.
Although its wrong but can some one please help me to know the reason for
it.
Strace says:
mprotect(0x7f872fb26000, 4096, PROT_READ) = 0
munmap(0x7f872fb0b000, 99154) = 0
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV (core dumped) +++
Tried searching in SO but to no success.

1 comment:

  1. You are getting a segmentation fault because printf is interpreting the 10 you passed it as a char *, and trying to read from machine address 10. On systems that run Linux, that address is not valid and causes a segmentation fault.

    ReplyDelete