This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/22485] pointer +- integer is never NULL


------- Additional Comments From gdr at integrable-solutions dot net  2005-07-15 08:10 -------
Subject: Re:  pointer +- integer is never NULL

"falk at debian dot org" <gcc-bugzilla@gcc.gnu.org> writes:

| ------- Additional Comments From falk at debian dot org  2005-07-15 06:41 -------
| Subject: Re:  pointer +- integer is never NULL
| 
| "gdr at integrable-solutions dot net" <gcc-bugzilla@gcc.gnu.org> writes:
| 
| > 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.
| 
| Sorry, I cannot follow you. I'd find it massively unsurprising if
| reinterpret_cast<int*>(0) produces a null pointer, and if I then get
| undefined behavior for doing something with it that is undefined for a
| null pointer.

But, if I used reinterpret_cast to turn an integer value 0 into a
pointer, there is no reason why the compiler would assume that I do not
know the underlying machine and what I'm doing with the pointer.

| In fact I'd find it very *surprising* if
| reinterpret_cast<int*>(0) behaves different than a normally
| constructed null pointer anywhere.

At least, you get that part of my indirect observation! :-)

| > Furthermore, given the mapping chosen by GCC, it takes even more
| > creative compiler to make (int *)0 + 5 also undefined.
| 
| And I don't see how that follows, either.

if follows from your surprise that reinterpret_cast<int*> does
something different from the null pointer constant (int*)0.

| As it seems, arguing with different levels of surprisingness seems to
| be somewhat subjective, so I don't think this leads us anywhere.

I'm not actually arguing on different level of surprisingness.  I'm
just looking at reinterpret_cast and its implication. 

| > 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.
| 
| This is a more relevant point. I don't think this optimization would
| break offsetof-like macros, since they'd use null pointer *constants*,
                                                            ^^^^^^^^^^^

For the offsetof *macro*, yes
But that is not the case for codes that uses
reinterpret_cat<int*>(expr), where expr is an integer expression with
value 0.  Scanning a region of memory starting from zero, is not
exactly the kind of thing never done in practice.

| which we could easily avoid to tag as non-null.

so you would have to pretend that a null pointer constant is not null?
That is even more bizarre arithmetic.

-- Gaby


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22485


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]