只读字符串 [英] Read-only string

查看:86
本文介绍了只读字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下字符串''str''是只读的?


char * str =" test string" ;;


无论如何''str''需要记忆。你认为,让''str''红色只有b $ b会获得性能吗?或者,它是对的?

解决方案



v4vijayakumar写道:

为什么以下字符串''str''是只读的?

char * str =" test string" ;;

无论如何''str''需要是在记忆中。你觉得,让''str''红色只会获得性能吗?或者,它是对的?




字符指针`str`是/不是/只读。您可以自由地为其分配任何

其他字符指针值。什么/是/只读是字符串

在内存中某处`str`指向的文字。


这样做的一个原因是它启用了嵌入式应用程序将

字符串文字放入(物理上)只读内存中。这可能会或可能不会
获得性能,但肯定可以节省RAM,这些系统往往需要额外支付价格溢价。


v4vijayakumar写道:

为什么以下字符串''str''是只读的?


因为C标准是这么说的。

(好吧,它说它是修改字符串的未定义行为

literal)char * str =" test string" ;;

无论如何''str''需要在内存中。你认为,制作''str''red-only
str与test string不一样,str可以修改:-)

会获得性能吗?或者,它是对的?



Le 21-06-2006,v4vijayakumar< v4 *********** @ yahoo。 COM> aécrit*:

为什么以下字符串''str''是只读的?

char * str =" test string";




因为这是定义语言的方式吗?

你可能想知道为什么语言是这样设计的。

我是不存在,但是我在编译中的几个知识让我认为,对于某些编译器来说,更容易做到

所以(它允许以只读方式存储它)记忆)。

Marc Boyer


why the following string, ''str'' is read-only?

char *str = "test string";

anyhow ''str'' needs to be in memory. do you think, making ''str'' red-only
would gain performance? or, it is right?

解决方案


v4vijayakumar wrote:

why the following string, ''str'' is read-only?

char *str = "test string";

anyhow ''str'' needs to be in memory. do you think, making ''str'' red-only
would gain performance? or, it is right?



Character pointer `str` is /not/ read-only. You are free to assign any
other character pointer value to it. What /is/ read-only is the string
literal somewhere in memory to which `str` points.

One reason for this is that it enables embedded applications to put the
string literal into (physically) read-only memory. This may or may not
gain performance, but certainly saves RAM which in these systems tends
to command a price premium.


v4vijayakumar wrote:

why the following string, ''str'' is read-only?
Because the C standard says so.
(well, it says it''s undefined behavior to modify a string
literal) char *str = "test string";

anyhow ''str'' needs to be in memory. do you think, making ''str'' red-only str is not the same thing as "test string", str can be modified :-)
would gain performance? or, it is right?



Le 21-06-2006, v4vijayakumar <v4***********@yahoo.com> a écrit*:

why the following string, ''str'' is read-only?

char *str = "test string";



Because that is the way the langage is defined ?
You could wonder why the langage is designed this way.
I was not there, but my few knoledges in compilation let
me assume that, for some compiler, it was easier to do
so (it allow to store it in read-only memory).

Marc Boyer


这篇关于只读字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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