代码错误但不正确 [英] Wrong-but-not-incorrect code

查看:86
本文介绍了代码错误但不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看到我最近看的一大堆代码(不是我的!):

--------

char * imgfilename [100] ;

sprintf((char *)imgfilename," mask%d.dib",params.profile);

ReadImage((char *)imgfilename);

--------

(ReadImage是程序代码的另一部分,完全符合理性读者所需的b / b
期待。)


对于CLC读者:

您可以通过人工构造或实际经验拿出

的东西那更'错了但仍然设法正确的代码

执行预定的任务?

dave


- -

Dave Vandervies dj******@csclub.uwaterloo.ca

确实,他们的选择并不总是与_I_

的选择一致,但那是因为他们是猫而我不是。

- 麦克安德鲁斯在可怕的魔鬼修道院中

Seen in a chunk of code I was looking at recently (not mine!):
--------
char* imgfilename[100];
sprintf((char*)imgfilename, "mask%d.dib", params.profile);
ReadImage((char*)imgfilename);
--------
(ReadImage is another part of the program''s code that does exactly what
the reasonable reader would expect.)

For the CLC readers:
Can you, by artifical construction or actual experience, come up with
something that''s more Wrong and yet still manages to be correct code
that performs the intended task?
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
It is true that their choices are not always consonant with the choices _I_
would make, but that''s because they''re cats and I''m not.
--Mike Andrews in the scary devil monastery

推荐答案




Dave Vandervies写道:


Dave Vandervies wrote:
看到我最近看的一大堆代码(不是我的!):
--------
char * imgfilename [100];
sprintf((char *)imgfilename," mask%d.dib",params.profile);
ReadImage((char *)imgfilename);
--------
(ReadImage是程序代码的另一部分,完全符合合理的读者所期望的。)

对于CLC读者:
你可以,通过人工建设或实际经验,提出一些更错误的东西,但仍然设法做出正确的代码
执行预定的任务?
Seen in a chunk of code I was looking at recently (not mine!):
--------
char* imgfilename[100];
sprintf((char*)imgfilename, "mask%d.dib", params.profile);
ReadImage((char*)imgfilename);
--------
(ReadImage is another part of the program''s code that does exactly what
the reasonable reader would expect.)

For the CLC readers:
Can you, by artifical construction or actual experience, come up with
something that''s more Wrong and yet still manages to be correct code
that performs the intended task?




我个人最喜欢的是这个单行同事
多年前发现的



#define HASHSIZE 51 / *小素数* /


-
Er ********* @ sun.com



My personal favorite is this one-liner a colleague
found many years ago:

#define HASHSIZE 51 /* a small prime */

--
Er*********@sun.com


>
我个人最喜欢的是多年前发现的一位同事:
#define HASHSIZE 51 / *一个小素数* /

-
Er*********@sun.com

他好奇地不知道3x10以上的3个时间表;-)

Allan
My personal favorite is this one-liner a colleague
found many years ago:

#define HASHSIZE 51 /* a small prime */

--
Er*********@sun.com
He obiously doesnt know his 3 time table above 3x10 ;-)
Allan



Dave Vandervies写道:
Dave Vandervies wrote:
看到我最近看到的一大堆代码(不是我的!):
--------
char * imgfilename [100];
sprintf((char *)imgfilename," mask%d.dib",params.profile);
ReadImage((char *)imgfilename);
--------
(ReadImage是程序代码的另一部分,完全符合合理的读者所期望的。)

对于CLC读者:
你能吗?通过人工建筑或实际经验,拿出
如此认为那更'错了但仍然设法执行预定任务的正确代码?
...
Seen in a chunk of code I was looking at recently (not mine!):
--------
char* imgfilename[100];
sprintf((char*)imgfilename, "mask%d.dib", params.profile);
ReadImage((char*)imgfilename);
--------
(ReadImage is another part of the program''s code that does exactly what
the reasonable reader would expect.)

For the CLC readers:
Can you, by artifical construction or actual experience, come up with
something that''s more Wrong and yet still manages to be correct code
that performs the intended task?
...




signed char方向; / * -1或+1 * /

无符号偏移量; / *绝对距离* /

T * ptr;


...

ptr + =方向*偏移;

/ *将指针移动到''direction'指定的方向'* /


一般情况下,代码不会按照预期的方式执行,因为

右侧将在

''unsigned''类型的范围内进行推广和评估。然而,在32位平台上,代码工作。 -

指针值缠绕并最终确定如果

代码按预期工作的情况。在64位平台上(32位无符号和

64位指针)代码开始失败的原因很明显。


- -

祝你好运,

Andrey Tarasevich



signed char direction; /* either -1 or +1 */
unsigned offset; /* absolute distance */
T* ptr;

...
ptr += direction * offset;
/* Moving the pointer in direction specified by ''direction'' */

In general case the code does not do what it was intended to do, since
the right hand side will be promoted and evaluated within the bounds of
''unsigned'' type. However, on a 32 bit platform the code "worked" - the
pointer value wrapped around and ended up exactly where it would be if
code worked as intended. On a 64 bit platform (32-bit ''unsigned'' and
64-bit pointers) the code started to fail for obvious reasons.

--
Best regards,
Andrey Tarasevich


这篇关于代码错误但不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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