修改C中的数据部分 [英] modifying the data section in C
问题描述
语句
char* c = "name";
原因 C
来指向数据段的位置。但是,为什么 C [0] ='N'
导致赛格故障?
causes c
to point to a location in data section. But why does c[0] = 'N'
cause a seg-fault?
为什么只读?
推荐答案
是的只读
关于字符串:
它指向静态地址
,其中字符串所在。它会大多只读
和全局的程序时,它将被加载。即使你尝试释放或修改,它会抛出分段故障
与内存保护
It points to static address
where string literals resides .It will be mostly readonly
and global to the program when it will be loaded . Even if you try to free or modify ,it will throw segmentation fault
on platforms with memory protection
ANSI C:
修改字符串为未定义行为
根据ANSI C标准
*的当编译包含此code程序,文字串放在标记为程序可执行文件只读
的部分; 的*
ANSI C :
modifying a string literal is undefined behavior
according to the ANSI C standard
*When the program containing this code is compiled, the literal string is placed in the section of the program executable file marked as readonly
;*
为了避免段错误
:
请字符串的本地副本。
这篇关于修改C中的数据部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!