下面的代碼段在編譯期間產生分段故障:字符數組初始化的產生分段故障
(GDB)運行
啓動程序:/home/anna/Desktop/a.out
接收 程序信號SIGSEGV,分段故障。
0xb7e97845中的strtok()從/lib/i386-linux-gnu/libc.so.6
#include <string.h>
#include <stdio.h>
main() {
char * sentence = "This is a sentence.";
char * words[200] ;
words[0] = strtok(sentence," ");
}
改變第五行之後,不會引發錯誤。
#include <string.h>
#include <stdio.h>
main() {
char sentence[] = "This is a sentence.";
char * words[200] ;
words[0] = strtok(sentence," ");
}
這是怎麼發生的?
注意:您正在執行程序,編譯已經成功創建了'a.out'文件。它在編譯過程中不會出現段錯誤。 – unwind
@WhozCraig這不是這個問題的重複 - 鏈接問題中的代碼實際上並沒有寫入字符串文本,儘管標題。 – interjay
@interjay好點。這個問題每天至少要問幾次。你會認爲這很容易找到。我會繼續搜索。謝謝。 – WhozCraig