dereferencing type-punned pointer will break strict-aliasing rules



I have a program for which this line:

if(! lstrtol(&atoken[2],length-2,(long *) &(lclparams->pad)) ||
(lclparams->pad< 0)){

generates the warning below, but ONLY if the gcc compiler is at -O2 or -O3. I don't see any reason why optimization should change things much in this piece of code - there's no way to optimize it out and I have verified that this particular line does what it should no matter how the program is compiled. Anyway, this is the warning:

warning: dereferencing type-punned pointer will break
strict-aliasing rules

The function lstrtol has this prototype:

int lstrtol(char *string,int length, long *ival);

and the pad field in lclparams in an int.

So two questions:

1. What doesn't the compiler like? Is it the cast of (long *) for
&(int_storage)?
2. Any ideas why the compiler only flags this when it's optimizing?
Seems like whatever the issue is it shouldn't have anything to do
with optimization levels.

I know that using a scratch long variable to retrieve the value
and then copying that into the int field eliminates the warning,
and it won't overflow as the values are going to be small integers, but
I am curious why the compiler acts this way.

Thanks,

David Mathog



.



Relevant Pages

  • Re: Parameter Name Warning?
    ... the names on the declarations do not matter, it should perhaps give a warning regarding different names being used. ... > there is more to those names than just what the compiler does with them. ... > You know only what you get in the header. ... >>> int main ...
    (microsoft.public.vc.language)
  • Re: is order urgent doubt
    ... Which *compiler* are you using? ... layout of the warning messages.) ... i = sizeof(long int); ... about the code in the editor vs. the code in the source file. ...
    (comp.lang.c)
  • Re: Whats the deal with size_t?
    ... ES> allowed to emit diagnostics that C doesn't require. ... in warning about a signed to unsigned conversion. ... a size_t to an int it is quite justified in warning about the reverse, ... The compiler is also quite justified in warning if you use a signed ...
    (comp.lang.c)
  • Re: Compendiums of compiler warning/error messages mapped to actual code problems?
    ... The general problem is of course that the compiler messages must be short, and that tends to make them so cryptic that it isn't always immediately obvious what the problem is. ... int this; ... Warning twarn.c: ...
    (comp.lang.c)
  • Re: matrix stuff (solving b = A*x) --> using numerical recipes
    ... but then I removed it since I couldn't see any compiler warnings/errors without stdio.h). ... turn the warning level up to the maximum ... void banmul(float **a, unsigned long n, int left, int right, float x, float b); ...
    (comp.lang.c)