所以,我有下面的程序:分割故障原因在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