断言必须采取int? [英] assert must take int?

查看:62
本文介绍了断言必须采取int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在C89中,我们是否必须将int作为参数传递给assert?我在代码断言时有代码

,例如:


断言(p);

,但我想知道我是否应该改为:


断言(0!= p);

-
$ b $bTomásóhéilidhe


In C89, do we have to pass an int as an argument to assert? I''ve got code
at the moment that does an assertion on pointer, e.g.:

assert(p);

, but I''m wondering if I should change that to:

assert(0 != p);
--
Tomás ó héilidhe

推荐答案

" Tom ?? s ?? H 18 ilidhe" < to *@lavabit.comwrites:
"Tom??s ?? h??ilidhe" <to*@lavabit.comwrites:

在C89中,我们是否必须将int作为参数传递给assert?我在代码断言时有代码

,例如:


断言(p);

,但我想知道我是否应该改为:


断言(0!= p);
In C89, do we have to pass an int as an argument to assert? I''ve got code
at the moment that does an assertion on pointer, e.g.:

assert(p);

, but I''m wondering if I should change that to:

assert(0 != p);



是的,在C89中原型用于int。在C99中,这已经被改为

到任何标量表达式(当然你不能把它写成

原型,但那是实现'的问题,不是你好)。


-

Ben。

Yes, in C89 the prototype is for an int. In C99 this has been changed
to any scalar expression (of course you can''t write that as a
prototype but that is the implementation''s problem, not yours).

--
Ben.


Tomásóhéilidhe说:
Tomás ó héilidhe said:

>

在C89中,我们是否必须将int作为参数传递给断言?我在代码断言时有代码

,例如:


assert(p);
>
In C89, do we have to pass an int as an argument to assert? I''ve got code
at the moment that does an assertion on pointer, e.g.:

assert(p);



4.2.1.1断言宏


概要


#include< ; assert.h>

void assert(int expression);


所以你必须提供一个int。如果你不这样做,行为是不明确的。


(你似乎已经猜到了,在C99中这已经改变了,而且只有表达

必须是标量 - 对int的限制已被解除。)

4.2.1.1 The assert macro

Synopsis

#include <assert.h>
void assert(int expression);

So you have to provide an int. If you don''t, the behaviour is undefined.

(As you seem to have guessed, in C99 this has changed, and the expression
only has to be scalar - the restriction to int has been lifted.)


>

,但我想知道我是否应该改为:


断言(0!= p);
>
, but I''m wondering if I should change that to:

assert(0 != p);



是的,你应该(或使用等效的断言(p!= 0)或断言(p!=

NULL)等等。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Yes, you should (or use an equivalent such as assert(p != 0) or assert(p !=
NULL) etc).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


Richard Heathfield< rj*@see.sig.invalidwrote在

新闻:c9 ****** ************************@bt.com:
Richard Heathfield <rj*@see.sig.invalidwrote in
news:c9******************************@bt.com:

>,但我想知道我是否应该改变它:

断言(0!= p);
>, but I''m wondering if I should change that to:

assert(0 != p);



是的,您应该(或使用等效的断言(p!= 0)或

断言(p!= NULL)等等)。


Yes, you should (or use an equivalent such as assert(p != 0) or
assert(p != NULL) etc).




感谢您的信息。我最近刚刚更改了

编译器时遇到了这个问题,我收到了一些关于转换的警告,从

指针到整数类型:-O


看起来我会做一个在文件中查找用于断言 :-P


(我不禁想到我以前的编译器实际上在做什么......是吧

将指针作为标量或是它在做什么东西......脏......)

-
$ b $bTomásóhéilidhe



Thanks for the info. I only came across the problem when I changed
compilers recently, I got a boat-load of warnings about conversions from
pointers to integer type :-O

Looks like I''ll be doing a "Find in files" for "assert" :-P

(I shudder to think what my previous compiler was actually doing... was it
taking the pointer as a scalar or was it doing something... dirty... )
--
Tomás ó héilidhe


这篇关于断言必须采取int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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