------- Additional Comments From gdr at integrable-solutions dot net 2005-07-14 23:03 ------- Subject: Re: pointer +- integer is never NULL
"falk at debian dot org" <[EMAIL PROTECTED]> writes: [...] | > | (In reply to comment #7) | > | | > | > I'm failing to find anything in the C++ standard that suggests that the | > | > following shall be undefined | > | > | > | > (reinterpret_cast<int*>(0) + 5) - 5 | > | | > | If (reinterpret_cast<int*>(0) + 5) - 5 is not undefined, then neither is | > | reinterpret_cast<int*>(0) + 5. Then what is its result, by which paragraph | > | in the standard? | > | > The standard says that the mapping used by reinterpret_cast to turn an | > integer into a pointer is *implemented-defined*. It is not undefined. | > GCC uses the "obvious" mapping, which is reinterpret_cast<int*>(0) is | > the null pointer. | | So your example boils down further to the question of whether | ((int*)0) + 5 is undefined, but you didn't answer my question | yet. No. I think I made an indirect observation. There is no requirement from the standard that (int *)0 is the same as reinterpret_cast<int*>(0) -- yes, I do not know many of practical compilers that do things differently, but it needs pointing out. Therefore, you cannot directly reduce reinterpret_cast<int*>(0) + 5 to (int *)0 + 5. However, for practical purposes, GCC uses the obvious mapping following the standard intent 5.2.10/4 for reinterpret_cast: [...] [Note: it is intended to be unsurprising to those who know the addressing structure of the underlying machine. ] My indirect observation was that reinterpret_cast is intended for specific needs that cannot adequately be expressed at the purely object type level. The result is intended to be unsurprising to those who know the addressing structure. Consequently it takes a creative compiler to make reinterpret_cast<int*>(0) + 5 undefined. Furthermore, given the mapping chosen by GCC, it takes even more creative compiler to make (int *)0 + 5 also undefined. There still are reasonable codes for system programming out there that needs the to go through the play with null pointer -- we, GCC, even used to distribute such things in the past. -- Gaby -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22485