Re: Quiting a program using pure C
[Specific to C99]
[color=blue]
>In <cmqjqc12r60@ne ws2.newsguy.com > Chris Torek <nospam@torek.n et> writes:[color=green]
>>... sort of a bug in C89, and is fixed in C99, where the argument
>>to assert() has type "bool" (from <stdbool.h>; really _Bool).[/color][/color]
In article <cmraof$57c$9@s unnews.cern.ch> Dan Pop <Dan.Pop@cern.c h> wrote:[color=blue]
>Nope, in C99 the argument to assert has scalar type.[/color]
Since I still have not actually bought the final PDF, I will believe
you; but:
[color=blue][color=green]
>>In C99, assert(NULL) is a valid invocation that -- if NDEBUG is not
>>defined -- emits a message and calls abort().[/color][/color]
[color=blue]
>You're contradicting yourself: if assert expected a bool argument, passing
>it (void *)0 would be far from correct: undefined behaviour.[/color]
I remember discussion (from before the C99 standard was final, I think)
about this. The claim was that:
a) assigning a bool variable from a pointer was allowed;
b) the result of such an assignment was "true" if the pointer
compares unequal to NULL and false if it compares equal -- e.g.:
char *p;
...
bool x = p; /* danger, maybe not actually legal C99 */
would always "mean the same thing" as:
bool x = (p != 0);
and therefore
c) by making assert "look like" a function taking a single "bool",
the rules for ordinary function parameter assignment would make:
p = malloc(N);
assert(p);
valid code (however ill-advised one might be to use assert() here).
If either (or both) of (a) or (b) is false in C99, the reasoning
would fall apart. (The pre-C99 draft I use as a quick searchable
text does not support either of the two claims, but I know that
this draft does not match the final standard, and that some of the
changes include details about "bool"s.)
In any case, I maintain that if you want to assert that some pointer
variable compares unequal to NULL, it is best -- for both correctness
*and* "human factor" reasons, as fuzzy and arguable as the latter
may be -- to write:
assert(ptr != NULL);
and not simply:
assert(ptr);
Of course, I also prefer:
if (ptr != NULL)
so "personal preference bias" might affect my "human factor reasons"
claim. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
[Specific to C99]
[color=blue]
>In <cmqjqc12r60@ne ws2.newsguy.com > Chris Torek <nospam@torek.n et> writes:[color=green]
>>... sort of a bug in C89, and is fixed in C99, where the argument
>>to assert() has type "bool" (from <stdbool.h>; really _Bool).[/color][/color]
In article <cmraof$57c$9@s unnews.cern.ch> Dan Pop <Dan.Pop@cern.c h> wrote:[color=blue]
>Nope, in C99 the argument to assert has scalar type.[/color]
Since I still have not actually bought the final PDF, I will believe
you; but:
[color=blue][color=green]
>>In C99, assert(NULL) is a valid invocation that -- if NDEBUG is not
>>defined -- emits a message and calls abort().[/color][/color]
[color=blue]
>You're contradicting yourself: if assert expected a bool argument, passing
>it (void *)0 would be far from correct: undefined behaviour.[/color]
I remember discussion (from before the C99 standard was final, I think)
about this. The claim was that:
a) assigning a bool variable from a pointer was allowed;
b) the result of such an assignment was "true" if the pointer
compares unequal to NULL and false if it compares equal -- e.g.:
char *p;
...
bool x = p; /* danger, maybe not actually legal C99 */
would always "mean the same thing" as:
bool x = (p != 0);
and therefore
c) by making assert "look like" a function taking a single "bool",
the rules for ordinary function parameter assignment would make:
p = malloc(N);
assert(p);
valid code (however ill-advised one might be to use assert() here).
If either (or both) of (a) or (b) is false in C99, the reasoning
would fall apart. (The pre-C99 draft I use as a quick searchable
text does not support either of the two claims, but I know that
this draft does not match the final standard, and that some of the
changes include details about "bool"s.)
In any case, I maintain that if you want to assert that some pointer
variable compares unequal to NULL, it is best -- for both correctness
*and* "human factor" reasons, as fuzzy and arguable as the latter
may be -- to write:
assert(ptr != NULL);
and not simply:
assert(ptr);
Of course, I also prefer:
if (ptr != NULL)
so "personal preference bias" might affect my "human factor reasons"
claim. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Comment