dereferencing type-punned pointer will break strict-aliasing rules
- From: David Mathog <mathog@xxxxxxxxxxx>
- Date: Tue, 03 Jul 2007 14:01:44 -0700
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
.
- Follow-Ups:
- Re: dereferencing type-punned pointer will break strict-aliasing rules
- From: Chris Torek
- Re: dereferencing type-punned pointer will break strict-aliasing rules
- From: Barry Schwarz
- Re: dereferencing type-punned pointer will break strict-aliasing rules
- From: Robert Gamble
- Re: dereferencing type-punned pointer will break strict-aliasing rules
- Prev by Date: Re: Model Facade
- Next by Date: Re: swprintf declaration inconsistencies?
- Previous by thread: swprintf declaration inconsistencies?
- Next by thread: Re: dereferencing type-punned pointer will break strict-aliasing rules
- Index(es):
Relevant Pages
|