473,549 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Dec 8 '07 #1
30 2148
"Tomás Ó hÉilidhe" <to*@lavabit.co mwrites:
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.
Dec 8 '07 #2
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
Dec 8 '07 #3
Richard Heathfield <rj*@see.sig.in validwrote in
news:c9******** *************** *******@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
Dec 8 '07 #4
In article <Xn************ *************** @194.125.133.14 >,
Toms hilidhe <to*@lavabit.co mwrote:
>(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
Dec 8 '07 #5
"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.

Dec 8 '07 #6
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?
Dec 8 '07 #7
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."
Dec 8 '07 #8
CJ
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.

Dec 8 '07 #9
"Tomás Ó hÉilidhe" <to*@lavabit.co mwrites:
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);
Apart from the question of legality, remember that the text of the
expression is printed as part of the error message. A message
containing "p ~= NULL" is going to be much clearer than one containing
just "p" (even if you use a clear name than "p").

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 8 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
3546
by: Fbio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a very early draft for a new "assert" syntax: This was inspired in Ruby's assert syntax. I'm not familiar with Ruby at all, so the chances are that...
7
3171
by: Stephen Tyndall | last post by:
I know the preprocessor is evil, but I'd like to know what's going on in the following code. The problem is when the num variable is used in the ASSERT macro inside main(). When running the code, I get the following error from Visual C++.NET 2003: warning C4806: '==' : unsafe operation: no value of type 'bool' promoted to type 'int' can...
11
2691
by: BigMan | last post by:
When should one prefer assert-ing to throwing an exception?
27
3809
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get failed assertions for assertions that at first thought ought to hold, but that's not important.) At the end is a full program containing the above...
5
3152
by: Alex Vinokur | last post by:
Here are two programs. --- foo1.c --- #include <assert.h> #define FOO 10 int main() { assert (15 < FOO); return 0; }
47
3068
by: Rob Thorpe | last post by:
In general, is it considered bad practice to use asserts in production code? What about writing a macro that does the same as assert but continues to work regardless of the state of NDEBUG? I can see that it would be poor style to use it for commonly encountered errors, but what about truly exceptional errors that would rarely if ever be...
13
9398
by: priyanka | last post by:
Hi there, Can anyone show me how the assert() function works ? I need to develop my own assert() function instead of using the one defined in the assert.h file. It would be great if anyone could show me the actual source code of assert() function in assert.h header file. I need to write the assert function that prints out error message...
29
2484
by: mailforpr | last post by:
Sometimes, I can't think of any good reason why I should have the program's logic thrown an exception. Except for catching the exception and printing "Uh, oh" to the screen. I also think that in most cases there's simply no way to handle an exception properly, because what can one do about an integer overflow? Reassign values? Restart the...
9
2244
by: Achintya | last post by:
Is it good to assert the pointer *each* time after a new() is called? or it should be a normal if condition check. which of below is good practice: (I know assert works only in debug) 1) ...... int* i; i = new int; assert( i ) ..... OR
0
7462
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7730
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7975
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5101
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3510
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1957
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1069
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.