修改C中的数据部分 [英] modifying the data section in C

查看:173
本文介绍了修改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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆