@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.
First parameter for printf()
is format string which is char *
pointer. So when you do printf(x)
it takes x
as char *
and tries to access string stored at address 10
. But its invalid so it gives segmentation fault.
之所以会出现分段错误,是因为printf
会将通过它的10解释为char *
,并试图从计算机地址10读取。在运行Linux的系统上,该地址无效并导致分段错误。
Use printf() in proper format. printf("%d",x).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.