This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Warning
- From: Andrew Haley <aph at redhat dot com>
- To: JJ <jj3666 at yahoo dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Mon, 12 May 2008 20:36:06 +0100
- Subject: Re: Warning
- References: <482895A2.5040603@yahoo.com>
JJ wrote:
> Hi, all!
>
> Regarding the following warning type...
>
> warning: dereferencing type-punned pointer will break strict-aliasing rules
>
> Ex1: object_array list;
> thing_array *things;
> -thing_array = (thing_array *)&list;
> +thing_array *things = (thing_array *)(void *)&list;
>
> Ex2: char_array audience;
> thing_array var_list;
> var_list = (thing_array&)audience;
>
> - From google'ing I found the work-a-round for Ex1 to remove the above
> warning,
Removing the warning won't solve your problem: it's a real bug and
may well cause your program to crash or worse.
> however I am having a hard time understanding how it relates to
> Ex2? Any help would be appreciated. It is legacy code that I am
> attempting to fix.
It's really hard to know because we don't know what the code does.
In general the rule is this: don't cast a pointer to type X to a
pointer to incompatible type Y. In practice there is never a
need to do this, but precisely how you fix it depends on the
specific program.
Andrew.