This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: pointer <-> integer round-up
- From: Fergus Henderson <fjh at cs dot mu dot OZ dot AU>
- To: Tom Lord <lord at emf dot net>
- Cc: gcc at gcc dot gnu dot org
- Date: Mon, 18 Mar 2002 18:20:34 +1100
- Subject: Re: pointer <-> integer round-up
- References: <200203151845.KAA24667@emf.net>
On 15-Mar-2002, Tom Lord <lord@emf.net> wrote:
>
> Just to summarize the ideas proposed regarding these warnings:
>
> The issue concerns warnings given (by default and with `-Wall') by
> recent versions of GCC for truncating conversions between integers and
> pointers. The controversial issue is whether or not such warnings should
> be given even when the conversion is marked with an explicit cast.
>
> 1. [...]
>
> Everything's fine. The warnings are perfect and very
> helpful. Live with it.
>
> 2. [...]
>
> Such warnings should not always occur for code containing an
> explicit cast. That they do makes the warning system less
> helpful. This is, after all, the traditional meaning of a
> cast that expresses a default conversion.
What exactly do you mean by "default conversion"?
Conversion from pointer to int or vice versa always require a cast.
If there is no such cast, then the code is not standard C; a diagnostic
is required, and it might well be an error rather than a warning.
So one cast is required to avoid an error. A second cast is
required to suppress the warning.
> Ways to selectively activate or de-activate these
> warnings (for explictly cast values) with compiler options
> would make everyone happy.
I think this point applies equally well to all the proposals.
AFAIK no-one has objected to adding additional compiler options to
enable or disable these warnings (I certainly didn't interpret
Richard Henderson's comments that way).
> 3. [...]
>
> Go with proposal (1). In code, use double casts to take
> pointers to a "wide enough" integer type, then a "narrow
> enough" integer type for pointer->int. Take integers
> to a "narrow enough" integer type, then a pointer type for
> int->pointer.
This is a straw man that over-complicates the real situation.
The way you describe it here would have pointer->int be a triple-cast!
You don't need both a "wide enough" integer type and a "narrow enough"
integer type. You just need one "wide enough" type, and casts
from pointers to integers or vice versa go via the "wide enough" type.
> True, nothing in the standard assures us that the narrow
> enough and wide enough integer types will both actually exist,
This is also a straw man. The standard does guarantee that a wide
enough type will exist.
It just doesn't guarantee that this type will be the right size to
avoid compiler warnings. That's because the standard doesn't say
anything about compiler warnings.
> 4. [...]
>
> Go with proposal (3) for now. However, in the event of a
> system where the suitable integer types are not available,
> detect the use of double casts as a special case, and
> supress the warning in those cases (on such platforms).
Or just suppress the warning in all cases (regardless of whether
there are double casts or not) on such platforms.
> a double cast on one of these future systems may
> wind up invoking a conversion that is not the same as
> the default conversion
This seems to be a new objection, not mentioned in your earlier
discussion.
Are you suggesting that you might get different results
if you use such a double cast rather than a single cast?
If so, could you sketch a potential implementation on which
this might be the case?
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.