【发布时间】:2010-12-18 21:18:31
【问题描述】:
所以我有以下程序:
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'这行怎么没有段错误?
【问题讨论】:
-
另见不同但相关的问题stackoverflow.com/questions/1770062/…
标签: c++ c linux segmentation-fault elf