是否定义了行为 [英] Is the behaviour defined

查看:63
本文介绍了是否定义了行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的同事认为

后续计划的行为是定义的,但我有点担心。


#include< stdio.h>

#include< string.h>


int main()

{

char * c;

c =& c;

strcpy(c," abc");

put(& c);

retun 0;

}


程序打印相同值abc在多个平台上,我甚至用b / b
尝试使用多个编译器。我可以看出它试图写入指向地址的
,所以可能它写的最大值是3个字节+

''\'''。但即使我尝试复制更多的4个字节,它也会打印整个

字符串而不会发生任何崩溃。


更改了行>> strcpy(c," abcdefg");


现在我知道这个行为是未定义的(写入超过4个字节)

作为指针的大小是4个字节(在我测试的机器上)。


任何人都可以发表评论这是否符合规范并且行为是保证。

保证。


谢谢

~

Hi,
A collegue of mine is of the opinion that the behaviour of the
following program is defined,but I am a little apprehensive.

#include<stdio.h>
#include<string.h>

int main()
{
char *c;
c = &c;
strcpy(c,"abc");
puts(&c);
retun 0;
}

The program prints the same value "abc" on multiple platforms and I even
tried it with multiple compilers.I can make out that its trying to write
to the pointer address and so probably the max it can write is 3 bytes +
''\0''.But even if I try to copy more that 4 bytes it prints the whole
string without any crashes.

Changed line >> strcpy(c,"abcdefg");

Now I know that this behaviour is undefined(writing more than 4 bytes)
as the sizeof the pointer is 4 bytes(on the machine I tested on).

Can anyone comment if this is compliant code and is the behaviour
guaranteed.

Thanks
~

推荐答案

> int main()
> int main()
{
char * c;
c =& c;
strcpy(c," abc");
puts (& c);
重新调整0;
}
{
char *c;
c = &c;
strcpy(c,"abc");
puts(&c);
retun 0;
}



应该返回0;

返回的咒语是在上面的测试程序中有错误。我没有在我编译的测试程序中有b $ b,但是在编写这封邮件的时候添加了它,担心会受到重创用C语言

纯粹主义者;)。


It should be return 0;
The spell of the return is wrong in the test program above.I did not
have it in my test program which I compiled , but added it while
composing this mail , of the fear of getting battered by the C language
purists ;).


grid在01/10/05写道:
grid wrote on 01/10/05 :
一位同事我认为以下
程序的行为是定义的,


至少,它是依赖于实现的。但是将指针中任何值放入

的结果可能会有一个未定义的结果(陷阱

表示)。有可能存在不确定的行为。

但我有点担心。


你可以!

#include< stdio.h>
#include< string.h>

int main()
{*> char * c;
c =& c;


类型不兼容。 (c是char *,& c是char **)

strcpy(c," abc");


''sizeof char * == sizeof" abc"''表达式的结果是

依赖于实现。


你需要一个4字节的空间用于abc。


例如,在x86的实模式(16位)模型中,指针是

16位宽,这意味着这台机器上有2个字节。对于abc来说太小了

放(& c);
重新调整0;
}
A collegue of mine is of the opinion that the behaviour of the following
program is defined,
At minimum, it''s implementation-dependent. But the result of putting
any value in a pointer may have an undefined result (trap
representation). There is a possibility of undefined behaviour.
but I am a little apprehensive.
You can!
#include<stdio.h>
#include<string.h>

int main()
{
char *c;
c = &c;
Types are incompatible. (c is a char * and &c is a char**)
strcpy(c,"abc");
The result of the ''sizeof char* == sizeof "abc"'' expression is
implementation-dependent.

You need a space of 4 bytes for "abc".

For example, on a x86 in real mode (16-bit) model small, pointers are
16-bit wide, which means 2 bytes on this machine. Too small for "abc"
puts(&c);
retun 0;
}




-

Emmanuel

C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

C库: http://www.dinkumware.com/refxc.html


..sig正在修理



--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair


" grid" < PR ****** @ gmail.com>在消息中写道

新闻:QK ************ @ news.oracle.com ...
"grid" <pr******@gmail.com> wrote in message
news:QK************@news.oracle.com...
我的同事属于认为
以下程序的行为已被定义,但我有点担心。

#include< stdio.h>
#include< string.h>

int main()
{*> char * c;
c =& c;
strcpy(c," abc");
puts(& c);
retun 0;
}
A collegue of mine is of the opinion that the behaviour of the
following program is defined,but I am a little apprehensive.

#include<stdio.h>
#include<string.h>

int main()
{
char *c;
c = &c;
strcpy(c,"abc");
puts(&c);
retun 0;
}




以上代码已损坏。你试图将4个字符/字节

(1 + strlen(" abc")== 4)存储到变量c中,但你不知道是否c

(指向char的指针)足以容纳4个字符/字节。在某些平台上

它可能是2甚至1,因此你有可能覆盖别的东西。

在建议代码之前先告诉你的同事更好地学习C比如

以上。


Alex



The above code is broken. You attempt to store 4 chars/bytes
(1+strlen("abc")==4) to the variable c, but you don''t know whether c
(pointer to a char) is big enough to hold 4 chars/bytes. On some platforms
it can be 2 or even 1, hence you''re risking to overwrite something else.
Tell your collegue first to learn C better before advising a code such as
the above.

Alex


这篇关于是否定义了行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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