Re: NULL with representation other then all bits 0
- From: Christian Bau <christian.bau@xxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 31 Jan 2006 09:53:23 +0000
In article <lnacdde01m.fsf@xxxxxxxxxxxxxxx>,
Keith Thompson <kst-u@xxxxxxx> wrote:
> Christian Bau <christian.bau@xxxxxxxxxxxxxxxxxxxx> writes:
> > In article <lnpsmau858.fsf@xxxxxxxxxxxxxxx>,
> > Keith Thompson <kst-u@xxxxxxx> wrote:
> [...]
> >> For example:
> >>
> >> char *null_ptr = 0; /* guaranteed to be a null pointer value */
> >> int zero = 0;
> >> char *maybe_null = (char*)zero; /* may or may not be a null pointer */
> >>
> >> The requirements for null pointer constants apply only to compile-time
> >> constructs.
> >
> > I believe this has been added as a requirement in C99.
>
> What has? Do you mean that maybe_null in the example is required to
> be a null pointer under C99?
>
> I don't believe that's correct. In particular, I don't believe the
> requirements have changed significantly between C90 and C99. (Of
> course I've been wrong before.) Can you provide chapter and verse?
>From the n1124 draft:
6.3.2.3 Pointers:
3. 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 anyobject or function.
5. An integer may be converted to any pointer type. Except as previously
specified, the result is implementation-defined, might not be correctly
aligned, might not point to an entity of the referenced type, and might
be a trap representation.
6.3.2.3.3 only talks about conversions, it doesn't talk about when
conversions happen (which has a special case for null pointer
constants). It says what the result of a conversion from an integer type
to a pointer type is in one special case. Note that we have here a full
conversion, and not a substitution.
I assume that the result of a conversion can only depend on the value
and the type of the thing that is converted, which implies that anything
that has a type and a value that could be the type and value of a null
pointer constant _must_ be converted to a null pointer. (I don't think
it is possible to have a null pointer constant of type char or short, so
converting a char or short of value 0 to pointer type might not be
guaranteed to produce a null pointer with this reasoning).
As an example, in
char* p = (char *) 0;
the special rules for null pointer constants are not invoked;
char* p = (char *) 1;
would be just as legal; it is just a conversion from int to char*. The C
Standard defines the result in the very strict case we have here, that a
null pointer constant is converted, but the result of a conversion
cannot depend on this fact. Zeroes that are not null pointer constants
are not mentioned in the rule, but they cannot be converted differently.
The result is implementation defined; an implementation would not be
free to define different results for
(char *) 1
(char *) (1 + 0)
(char *) (int) (1.73f)
because they all convert the same value. Neither can it define different
results for different zeroes.
.
- Follow-Ups:
- Re: NULL with representation other then all bits 0
- From: Keith Thompson
- Re: NULL with representation other then all bits 0
- References:
- NULL with representation other then all bits 0
- From: yossi . kreinin
- Re: NULL with representation other then all bits 0
- From: Christian Bau
- Re: NULL with representation other then all bits 0
- From: Keith Thompson
- Re: NULL with representation other then all bits 0
- From: Christian Bau
- Re: NULL with representation other then all bits 0
- From: Keith Thompson
- NULL with representation other then all bits 0
- Prev by Date: Re: What u mean by this statemnet ?.
- Next by Date: Re: struct compatibility
- Previous by thread: Re: NULL with representation other then all bits 0
- Next by thread: Re: NULL with representation other then all bits 0
- Index(es):
Relevant Pages
|