可写字符串 [英] Writable strings

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

问题描述

Hello C coders,et。 al,


请看下面的代码片段。


01 int main(无效)

02 {

03 char mystr [] =" Hello world \ n";

04 char * pmystr2 =" My String \ n";

05

06 mystr [0] = 0;

07 pmystr2 [0] = 0;

08

09返回0;

10}


第7行是否允许第二次使用''pmystr2'?应该

对Hello world \ n的问题没有问题。因为它被分配给

第3行的char数组,但是第二个实例可能正在写入一段标记为只读的内存部分

,其中存在问题最初发生在第4行。


或者,编译器是否需要采用常量字符串My

String在第4行,将它复制到可写的地方,然后将

pmystr2分配到新部分的地址?


任何见解都会有所帮助。


杰森。

Hello C coders, et. al,

Please have a look at the snippet of code below.

01 int main(void)
02 {
03 char mystr[] = "Hello world\n";
04 char *pmystr2 = "My String\n";
05
06 mystr[0] = 0;
07 pmystr2[0] = 0;
08
09 return 0;
10 }

Is the second assignment with ''pmystr2'' allowed at line 7? There should
be no problem with "Hello world\n" as it is assigned to a char array on
line 3, but the second instance might be writing to a section of memory
that is marked read only, in which the problem originally occurs at line 4.

Or, would the compiler be required to take the constant string "My
String" on line 4, copy it to somewhere it is writable, and then assign
pmystr2 to the address in the new section?

Any insights would help.

Jason.

推荐答案

2005年6月22日星期三12:32:40 +0200,Jason Curl写道:
On Wed, 22 Jun 2005 12:32:40 +0200, Jason Curl wrote:
Hello C coders,et。 al,

请看下面的代码片段。

01 int main(void)
02 {
03 char mystr [] =" Hello world \ n";
04 char * pmystr2 =" My String\\\
";
05
06 mystr [0] = 0;
07 pmystr2 [0] = 0;
08
09返回0;
10}

第7行是否允许使用''pmystr2'进行第二次作业? Hello world \ n应该没有问题。因为它被分配给第3行的char数组,但是第二个实例可能正在写入标记为只读的内存部分,其中问题最初发生在第4行。


修改字符串文字会导致未定义的行为,所以你说

第7行无效。

或者编译器是需要采用常量字符串My
String在第4行,将它复制到可写的地方,然后将
pmystr2分配到新部分的地址?
Hello C coders, et. al,

Please have a look at the snippet of code below.

01 int main(void)
02 {
03 char mystr[] = "Hello world\n";
04 char *pmystr2 = "My String\n";
05
06 mystr[0] = 0;
07 pmystr2[0] = 0;
08
09 return 0;
10 }

Is the second assignment with ''pmystr2'' allowed at line 7? There should
be no problem with "Hello world\n" as it is assigned to a char array on
line 3, but the second instance might be writing to a section of memory
that is marked read only, in which the problem originally occurs at line 4.
Modifying a string literal results in undefined behaviour, so as you say
line 7 is invalid.
Or, would the compiler be required to take the constant string "My
String" on line 4, copy it to somewhere it is writable, and then assign
pmystr2 to the address in the new section?




不,它可以''这样做是因为第7行不允许改变指针pmystr2的值。


劳伦斯



No, it can''t do that because line 7 is not allowed to change the value of
the pointer pmystr2.

Lawrence

Lawrence Kirby写道:
Lawrence Kirby wrote:
2005年6月22日星期三12:32:40 +0200,Jason Curl写道:

On Wed, 22 Jun 2005 12:32:40 +0200, Jason Curl wrote:

你好C编码器等。 al,
Hello C coders, et. al,



[snip]

修改字符串文字导致未定义的行为,因此你说第7行无效。


[snip]

Modifying a string literal results in undefined behaviour, so as you say
line 7 is invalid.




未定义的行为不一定无效。它可能不是便携式的,但如果你知道你的编译器正在做什么(并且希望它很好

记录),那么就没有任何内在的错误用这个代码。对于

的例子,如果你的内存预算非常紧张,并且你知道一个事实

,编译器会生成适用于第7行的代码(而且你是意识到

的事实是它不便携,但即使你不知道),那么

第7行也没问题。

从理由开始:


未定义的行为使实施者许可证不会捕获难以诊断的某些

程序错误。它还标识了可能符合语言扩展的区域:实现者可以通过提供官方未定义的

行为的定义来增加语言。

如果执行者决定允许第7行,那么就没有错了




Undefined behaviour is not necessarily invalid. It may not be portable,
but if you know what your compiler is doing (and hopefully it''s well
documented), then there''s nothing inherently wrong with this code. For
example, if you have a very tight memory budget, and you know for a fact
that the compiler generates code that works for line 7 (and you''re aware
of the fact that it''s not portable, but even if you''re not aware), then
there''s no problem with line 7.
From the rationale:

Undefined behavior gives the implementor license not to catch certain
program errors that are difficult to diagnose. It also identifies areas
of possible conforming language extension: the implementor may augment
the language by providing a definition of the officially undefined
behavior.
If the implementor decided to allow line 7, then there''s nothing wrong
with that.




" Omri Barel" < SP ***** @ b2z.com>写了

"Omri Barel" <sp*****@b2z.com> wrote

未定义的行为使实现者许可证不会捕获难以诊断的某些程序错误。它还标识了可能符合语言扩展的区域:实现者可以通过提供官方未定义行为的定义来扩充
语言。

Undefined behavior gives the implementor license not to catch certain
program errors that are difficult to diagnose. It also identifies areas
of possible conforming language extension: the implementor may augment the
language by providing a definition of the officially undefined behavior.



实现定义的行为是为此目的而提供的。当然

没有什么可以防止编译器供应商稍微改变C / /
语言来制作那些本来就是非法合法的程序

技术上是一个新版本。


Implementation-defined behaviour is provided for that purpose. Of course
there''s nothing to prevent a compiler vendor from slightly altering the C
language to make programs which would have been illegal legal in what is now
technically a new version.


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

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