Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > assert must take int?

Reply
Thread Tools

assert must take int?

 
 
Toms hilidhe
Guest
Posts: n/a
 
      12-08-2007

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);


--
Toms hilidhe
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-08-2007
"Tomás Ó hÉilidhe" <(E-Mail Removed)> writes:

> 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);


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.
 
Reply With Quote
 
 
 
 
Toms hilidhe
Guest
Posts: n/a
 
      12-08-2007
Richard Heathfield <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

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

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




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"

(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... )


--
Toms hilidhe
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-08-2007
Toms hilidhe said:

>
> 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 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.)

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


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
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      12-08-2007
In article <Xns9A007F5B8DD68toelavabitcom@194.125.133.14>,
Toms hilidhe <(E-Mail Removed)> wrote:
>(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... )


Your previous compiler was doing what most compilers have always done:
define assert as a macro something like

#define assert(e) \
((void) ((e) ? 0 : __assert (#e, __FILE__, __LINE__)))

(that's the definition in MacOS X). The asserted expression is used
as a conditional, so it works perfectly well with a pointer. That
this is the Right Thing is confirmed by the change in C99.

The restriction in C90 was clearly a mistake; I'm not sure where it
came from.

-- Richard
--
:wq
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-08-2007
"Tom��������������� ���������������� " wrote:
> In C89, do we have to pass an int as an argument to assert?


No, you need to pass an expression that yields a value of any integer
type (and some implementations accept any scalar type, although that is
not guaranteed).
> I've got code
> at the moment that does an assertion on pointer, e.g.:
>
> assert(p);


Since pointers are not of an integer type, this is not a good idea.

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


That is probably a good idea.
It is also a good idea to remember that the result of a failed assert
can often be extreme consternation, puzzlement, and anger on the part of
a user. Remember to define NDEBUG for any code outside of the testing
stage, even if you consider using the assert macro a good debugging
technique.

 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      12-08-2007
On Sat, 08 Dec 2007 12:31:35 +0000, Richard Heathfield wrote:
> Tomás Ó hÉilidhe said:
>> 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 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.


The "prototype" would allow for an implicit conversion to int, if there
isn't any statement suggesting otherwise, so it would mean that C90
requires assert(0.5) to fail. It would also mean that assert(p); is a
constraint violation, rather than undefined behaviour. Does C90 have
relevant text on either of these points in what follows?
 
Reply With Quote
 
Golden California Girls
Guest
Posts: n/a
 
      12-08-2007
Martin Ambuhl wrote:
> "Tom��������������� ���������������� " wrote:
>> In C89, do we have to pass an int as an argument to assert?

>
> No, you need to pass an expression that yields a value of any integer
> type (and some implementations accept any scalar type, although that is
> not guaranteed).
>> I've got code at the moment that does an assertion on pointer, e.g.:
>>
>> assert(p);

>
> Since pointers are not of an integer type, this is not a good idea.
>
>>
>> , but I'm wondering if I should change that to:
>>
>> assert(0 != p);

>
> That is probably a good idea.
> It is also a good idea to remember that the result of a failed assert
> can often be extreme consternation, puzzlement, and anger on the part of
> a user. Remember to define NDEBUG for any code outside of the testing
> stage, even if you consider using the assert macro a good debugging
> technique.


Or perhaps rewriting it so it says something more like "Call 1-800-HELP-DESK and
read us the rest of this message."
 
Reply With Quote
 
CJ
Guest
Posts: n/a
 
      12-08-2007
On 8 Dec 2007 at 12:31, Richard Heathfield wrote:
> Toms hilidhe said:
>
>>
>> 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 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.)
>
>>
>> , but I'm wondering if I should change that to:
>>
>> assert(0 != p);

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


But can't we get from the Standard the following two facts:
1) any pointer can be converted (possibly with loss of information) to
an int
2) the value of such a converted pointer is 0 if and only if the pointer
was NULL

In this case all will be well - if there's a prototype in scope then the
compiler will automatically convert the pointer to an int, and 2)
guarantees that the assert behaves as intended.

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-08-2007
CJ wrote:

> But can't we get from the Standard the following two facts:
> 1) any pointer can be converted (possibly with loss of information) to
> an int


There's something like that in the standard.
Either that, or the attempt can be undefined.

N869
6.3.2.3 Pointers
[#6] Any pointer type may be converted to an integer type.
Except as previously specified, the result is
implementation-defined. If the result cannot be represented
in the integer type, the behavior is undefined. The result
need not be in the range of values of any integer type.

> 2) the value of such a converted pointer
> is 0 if and only if the pointer was NULL


No.
There's nothing like that in the standard.

N869
6.3.2.3 Pointers
[#3] An integer constant expression with the value 0, or
such an expression cast to type void *, is called a null
pointer constant. If a null pointer constant is
converted to a pointer type, the resulting pointer, called a
null pointer, is guaranteed to compare unequal to a pointer
to any object or function.


--
pete
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To assert or not to assert... ImpalerCore C Programming 79 05-17-2010 12:47 PM
Why must and must not be "final" ? NeoGeoSNK Java 25 11-24-2006 02:02 AM
assert 0, "foo" vs. assert(0, "foo") Thomas Guettler Python 3 02-23-2005 07:53 PM
assert(x) and '#define ASSERT(x) assert(x)' Alex Vinokur C Programming 5 11-25-2004 08:48 PM
RE: remove assert statement (Was: Re: PEP new assert idiom) Robert Brewer Python 1 11-07-2004 06:53 PM



Advertisments