0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[]) {
printf("hello dude\n");
//cause segfault
*(int*)0 = 0;
return 0;
}
gcc test.c -o test
./test &> out.txt
Segmentation fault (core dumped)
./test > out.txt
Segmentation fault (core dumped)
./test 1> out.txt
Segmentation fault (core dumped)
打開文本文件,並且「hello dude」從不寫入。如果你註釋掉導致segfault的行,那麼「hello dude」會被寫入文件。一些segfault如何中斷stdout。有什麼可以在命令行上捕獲輸出嗎?我嘗試了cygwin bash和linux bash shell。C程序時,段錯誤標準輸出重定向未被捕獲到文本文件中
你爲什麼投這個int指針爲0,並試圖去參考,並指派0呢?它指向的是它並不擁有的正確內存,因此它會因爲段錯誤而崩潰。 – t0mm13b
@ t0mm13b這是意圖。 –
也注意到重定向,它不是真的指向一個文件,看&符號,你運行這個作爲後臺工作。 – t0mm13b