c++ - Can the expression "(ptr == 0) != (ptr == (void*)0)" really be true? -
i read claim in a forum thread linked in comment @jsantander:
keep in mind when assign or compare pointer zero, there special magic occurs behind scenes use correct pattern given pointer (which may not zero). 1 of reasons why things
#define null (void*)0
evil – if comparechar*
null
magic has been explicitly (and unknowingly) turned off, , invalid result may happen. clear:(my_char_ptr == 0) != (my_char_ptr == (void*)0)
so way understand it, architecture null pointer is, say, 0xffff
, code if (ptr)
, compare ptr 0xffff
instead of 0.
is true? described c++ standard?
if true, mean 0 can safely used architectures have non-zero null pointer value.
edit
as clarification, consider code:
char *ptr; memset(&ptr, 0, sizeof(ptr)); if ((ptr == (void*)0) && (ptr != 0)) { printf("it can happen.\n"); }
this how understand claim of forum post.
there's 2 parts question. i'll start with:
if true, mean 0 can safely used architectures have non-zero null pointer value.
you mixing "value" , "representation". value of null pointer called null pointer value. representation bits in memory used store value. representation of null pointer anything, there no requirement all-bits-zero.
in code:
char *p = 0;
p
guaranteed null pointer. might not have all-bits-zero.
this no more "magic" code:
float f = 5;
f
not have same representation (bit-pattern in memory) int 5
does, yet there no problem.
the c++ standard defines this. text changed in c++11 addition of nullptr
; in versions of c , c++, integer literal 0
when converted pointer type generates null pointer.
from c++11:
a null pointer constant integral constant expression prvalue of integer type evaluates 0 or prvalue of type std::nullptr_t. null pointer constant can converted pointer type; result null pointer value of type , distinguishable every other value of object pointer or function pointer type. such conversion called null pointer conversion.
0
null pointer constant, , (char *)0
example null pointer value of type char *
.
it's immaterial whether null pointer has all-bits-zero or not. matters null pointer guaranteed generated when convert integral constexpr of value 0
pointer type.
moving onto other part of question. the text quoted complete garbage through , through. there's no "magic" in idea conversion between types results in different representation, discuss above.
the code my_char_ptr == null
guaranteed test whether or not my_char_ptr
null pointer.
it evil if write in own source code, #define null (void*)0
. because undefined behaviour define macro might defined standard header.
however, standard headers can write whatever standard requirements null pointers fulfilled. compilers can "do magic" in standard header code; example there doesn't have file called iostream
on filesystem; compiler can see #include <iostream>
, have hardcoded of information standard requires iostream
publish. obvious practical reasons, compilers don't this; allow possibility independent teams develop standard library.
anyway, if c++ compiler includes #define null (void *)0
in own header, , result non-conforming happens, compiler non-conforming obviously. , if nothing non-conforming happens there no problem.
i don't know text quote direct "is evil" comment at. if directed @ compiler vendors telling them not "evil" , put out non-conforming compilers, guess can't argue that.
Comments
Post a Comment