So I have the following program:
int main(){
char* one = "computer";
char two[] = "another";
two[1]='b';
one[1]='b';
return 0;
}
It segfaults on the line "one[1]='b'" which makes sense because the memory that the pointer "one" points to must be in read only memory. However, the question is why doesn't the line "two[1]='b'" segfault? Looking at the assembly output from gcc:
.file "one.c"
.section .rodata
.LC0:
.string "computer"
.LC1:
.string "another"
.text
.globl main
.type main, @function
main:
We see that both strings are in the rodata section so they are readonly. So then how come the line "two[1]='b' does not segfault?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…