This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
pointer <-> integer conversion warnings (bogus -Wall warnings)
- From: Tom Lord <lord at emf dot net>
- To: gcc at gcc dot gnu dot org
- Cc: dewar at gnat dot com, rth at redhat dot com
- Date: Wed, 13 Mar 2002 13:59:03 -0800 (PST)
- Subject: pointer <-> integer conversion warnings (bogus -Wall warnings)
- References: <20020313000641.72845F2DD2@nile.gnat.com> <200203131212.EAA27998@morrowfield.home>
So, there's a warning about:
cast to pointer from integer of a different size
which gets set off even if there is an explicit cast. rth suggests:
x = (void *)(size_t)y
and I think that's pretty nice. One can safely assume that `size_t'
is small enough not to overflow a pointer type and the double cast
(aside from being easy to grep for) means "Yes, I really mean to
truncate the integer if necessary before casting it to a pointer."
Now, what about this one:
cast from pointer to integer of a different size
The code in question, once again, has an explicit cast:
x = (int)p;
I am assured by the flow through my program that the value stored in
`p' was first cast from an integer and that it's range is sufficiently
constrained that it won't cause a problem on any even vaguely sane
architecture. I added the cast to the code precisely to eliminate
the warnings and to signal that there's a possible value truncation
here that can't be disproven by the compiler -- but I want that
well-defined conversion anyway.
There is no integer type guaranteed to be wide enough to hold values
from pointers. So unlike the first warning I asked about, I don't see
any reasonable "double cast" that solves the problem.
Have I overlooked something or is -Wall giving a bogus warning?
-t