所以我有以下程序:
int main(){
char* one = "computer";
char two[] = "another";
two[1]='b';
one[1]='b';
return 0;
}
它在“one [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'”这一行怎么没有段错误呢?