[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: NULL pointer arithmetic issues
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
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 22.214.171.124.)
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 126.96.36.199...
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:
"The macro NULL is defined in <stddef.h> (and other headers) as a null
pointer constant; see 7.17."
So, 0 casted as a pointer gives a NULL pointer.
And paragraph 6 says:
"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
And I can only read the "previously specified" to refer to the
equivalence between a NULL pointer and integer 0, because nothing before
paragraph 6 talks about pointer to integer, so I can't see how it can be
read as something more specific than all the things mentioned in the
prebious 6 paragraphs.
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
Main Index |
Thread Index |