tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: NULL pointer arithmetic issues



On 2020-02-25 00:24, Johnny Billquist wrote:
On 2020-02-24 23:35, Mouse wrote:
Unless I remember wrong, older C standards explicitly say that the
integer 0 can be converted to a pointer, and that will be the NULL
pointer, and a NULL pointer cast as an integer shall give the value
0.

The only one I have anything close to a copy of is C99, for which I
have a very late draft.

Based on that:

You are not quite correct.  Any integer may be converted to a pointer,
and any pointer may be converted to an integer - but the mapping is
entirely implementation-dependent, except in the integer->pointer
direction when the integer is a "null pointer constant", defined as
"[a]n integer constant expression with the value 0" (or such an
expression cast to void *, though not if we're talking specifically
about integers), in which case "the resulting pointer, called a null
pointer, is guaranteed to compare unequal to a pointer to any object or
function".  You could have meant that, but what you wrote could also be
taken as applying to the _run-time_ integer value 0, which C99's
promise does not apply to.  (Quotes are from 6.3.2.3.)

I don't think there is any promise that converting a null pointer of
any type back to an integer will necessarily produce a zero integer.

Maybe we are reading things differently...?

Looking at 6.3.2.3...

As far as I read, paragraph 3 says:

"An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.55) 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."

Essentially, the integer constant 0 can be casted to a pointer, and that pointer is then a null pointer constand, also called a null pointer. And footnote 55 says:

Oh. And I actually do not believe it has to be a constant. The text says "integer constant expression with the value 0, or such an expression..."

So either a constant expression, or just an expression, which gives a 0, can be cast to a pointer, that that will be the NULL pointer.

(I realized when reading, that I might have implied that it only applied to constanst, which I think it does not.)

But I might have misunderstood everything, of course...

  Johnny

--
Johnny Billquist                  || "I'm on a bus
                                  ||  on a psychedelic trip
email: bqt%softjar.se@localhost             ||  Reading murder books
pdp is alive!                     ||  tryin' to stay hip" - B. Idol


Home | Main Index | Thread Index | Old Index