所以,我有下面的程序:分割故障原因在Linux
int main(){
char* one = "computer";
char two[] = "another";
two[1]='b';
one[1]='b';
return 0;
}
它出現segfaults上這是有意義的行「一個[1] =‘B’」,因爲存儲器「one」指針必須位於只讀存儲器中。然而,問題是爲什麼不行「two [1] ='b'」段錯誤?查看gcc的彙編輸出:
.file "one.c"
.section .rodata
.LC0:
.string "computer"
.LC1:
.string "another"
.text
.globl main
.type main, @function
main:
我們看到兩個字符串都位於rodata節中,因此它們是隻讀的。那麼爲什麼線「two [1] ='b'沒有段錯誤?
也看到了不同,但相關的問題http://stackoverflow.com/questions/ 1770067 /定義指針到靜態字符串/ 1770067#1770067 – 2009-11-20 20:43:14