int/unsigned long * idiocy

  • Thread starter Christopher Benson-Manica
  • Start date
K

Kenneth Brody

Dan Pop wrote:
[...]
Well written C code always uses NULL, because it expresses the
programmer's intentions better than any explicit null pointer constant.
After all, this is why this macro was introduced in the first place.

Am I correct that all-bits-zero is not necessarily the same as NULL?

In other words, this is not guaranteed to get a set of 10 NULL pointers
(assuming the calloc succeeds):

char **pointers = calloc(10,sizeof(*pointers));

--

+---------+----------------------------------+-----------------------------+
| Kenneth | kenbrody at spamcop.net | "The opinions expressed |
| J. | http://www.hvcomputer.com | herein are not necessarily |
| Brody | http://www.fptech.com | those of fP Technologies." |
+---------+----------------------------------+-----------------------------+
 
K

Kenneth Brody

:
[...]
I know that what you said is literally true, and the Standard
supports (void*)0 as a "second" null pointer constant, but really
it's just a corollary of the fact that 0 is a null pointer constant.
In C, (void*) can be implicitly converted to any other pointer type,
so (void*)0 and 0 are exactly interchangeable in pointer contexts.

But, it prevents NULL from being passed in a non-pointer context.
I recently saw a post here (or c.l.c.m) using NULL instead of '\0'.
If NULL is defined as "0", this wouldn't cause a complaint, but if
it's defined "((void *)0)", it should flag a warning for you, which
(IMHO) is probably better.

[...]

--

+---------+----------------------------------+-----------------------------+
| Kenneth | kenbrody at spamcop.net | "The opinions expressed |
| J. | http://www.hvcomputer.com | herein are not necessarily |
| Brody | http://www.fptech.com | those of fP Technologies." |
+---------+----------------------------------+-----------------------------+
 
C

Clark Cox

Kenneth Brody said:
Dan Pop wrote:
[...]
Well written C code always uses NULL, because it expresses the
programmer's intentions better than any explicit null pointer constant.
After all, this is why this macro was introduced in the first place.

Am I correct that all-bits-zero is not necessarily the same as NULL?

Yes.
 
C

CBFalconer

Kenneth said:
Dan Pop wrote:
[...]
Well written C code always uses NULL, because it expresses the
programmer's intentions better than any explicit null pointer
constant. After all, this is why this macro was introduced in
the first place.

Am I correct that all-bits-zero is not necessarily the same as
NULL?

In other words, this is not guaranteed to get a set of 10 NULL
pointers (assuming the calloc succeeds):

char **pointers = calloc(10,sizeof(*pointers));

Definitely not guaranteed. You need something like:

int i;
char* *pointers;

if (!(pointers = malloc(10 * sizeof *pointers))) {
/* handle malloc failure */
}
else {
for (i = 0; i < 10; i++) pointer = NULL;
}
 
M

Mike Wahler

Dan Pop said:
In <[email protected]> nrk

Except that these functions don't take pointers to flags, they just take
the flags. I have yet to figure a good reason for requiring a pointer
to flags, unless the function is supposed to modify the user specified
flags (which would be really weird, as an API design).

Actually, some Windows API functions do exactly that.
Weird? You be the judge. :)

-Mike
 
K

Keith Thompson

Nope, it isn't.


Nope, they aren't: the result of converting an integer to a pointer is
*implementation-defined*, so, a priori, (void*)0 can be *anything*. Its
special semantics come *exclusively* from its *explicit* inclusion in the
definition of the null pointer constant.

The fact that (void*)0 is a "null pointer constant" does come
exclusively from its explicit inclusion in the definition. But even
if the definition of "null pointer constant" didn't mention void*, the
result of evaluating the expression (void*)0 would still be a null
pointer. (On the other hand, the result of evaluating (void*)x, where
x is an integer variable with the value 0, isn't necessarily a null
pointer.)

It's also important to keep in mind that a "null pointer constant" is
a construct in a source program, and a "null pointer" (which can be
obtained by evaluating a null pointer constant or by a number of other
means) is a value that exists at run time.

Here's the relevant paragraph, C99 6.3.2.3 p3 (using underscores to
denote italics):

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.

and p4:

Conversion of a null pointer to another pointer type yields a null
pointer of that type. Any two null pointers shall compare equal.

So given
int zero = 0;

we have:

0 /* a null pointer constant */
(void*)0 /* a null pointer constant */
(int*)0 /* not a null pointer constant, but evaluates
to a null pointer value */
(void*)zero /* not a null pointer constant, may or may not
evaluate to a null pointer value */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top