Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

c - Does Standard define null pointer constant to have all bits set to zero?

( I'm quoting ISO/IEC 9899:201x )

Here we see that, integer constant expression has an integer type:

6.6 Constant expressions

6. An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, _Alignof expressions, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof or _Alignof operator.

Then this holds true for any integer type:

6.2.6.2 Integer types

5. The values of any padding bits are unspecified.A valid (non-trap) object representation of a signed integer type where the sign bit is zero is a valid object representation of the corresponding unsigned type, and shall represent the same value. For any integer type, the object representation where all the bits are zero shall be a representation of the value zero in that type.

Then we see that a null pointer constant is defined using an integer constant expression with the value 0.

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 any object or function.

Therefore the null pointer constant must have all it's bits set to zero.

But there are many answers online and on StackOverflow that say that that isn't true.

I have a hard time believing them given the quoted parts.

( Please answer using references to the latest Standard )

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Does Standard define null pointer constant to have all bits set to zero?

No, it doesn't. No paragraph of the C Standard impose such a requirement.

void *p = 0;

p for example is a null pointer, but the Standard does not require that the object p must have all bit set.

For information the c-faq website mentions some systems with non-zero null pointer representations here: http://c-faq.com/null/machexamp.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...