Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   assert must take int? (http://www.velocityreviews.com/forums/t558293-assert-must-take-int.html)

Toms hilidhe 12-08-2007 11:57 AM

assert must take int?
 

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

Ben Bacarisse 12-08-2007 12:23 PM

Re: assert must take int?
 
"Tomás Ó hÉilidhe" <toe@lavabit.com> 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.

Toms hilidhe 12-08-2007 12:31 PM

Re: assert must take int?
 
Richard Heathfield <rjh@see.sig.invalid> wrote in
news:c9Wdnc52HeltEMfanZ2dnUVZ8uidnZ2d@bt.com:

>> , 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" :-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... )


--
Toms hilidhe

Richard Heathfield 12-08-2007 12:31 PM

Re: assert must take int?
 
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

Richard Tobin 12-08-2007 03:43 PM

Re: assert must take int?
 
In article <Xns9A007F5B8DD68toelavabitcom@194.125.133.14>,
Toms hilidhe <toe@lavabit.com> 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

Martin Ambuhl 12-08-2007 07:37 PM

Re: assert must take int?
 
"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.


Harald van Dijk 12-08-2007 08:28 PM

Re: assert must take int?
 
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?

Golden California Girls 12-08-2007 10:04 PM

Re: assert must take int?
 
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."

CJ 12-08-2007 10:15 PM

Re: assert must take int?
 
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.


pete 12-08-2007 10:31 PM

Re: assert must take int?
 
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


All times are GMT. The time now is 04:06 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.