Re: dereferencing type-punned pointer will break strict-aliasing rules
- From: Robert Gamble <rgamble99@xxxxxxxxx>
- Date: Tue, 03 Jul 2007 21:30:41 -0000
On Jul 3, 5:01 pm, David Mathog <mat...@xxxxxxxxxxx> wrote:
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)?
Yes.
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.
The example breaks the aliasing rules and hence invokes undefined
behavior so anything is allowed including working properly in one case
and giving you a diagnostic in another. The point of the aliasing
rules is to allow the compiler to perform certain optimizations by
assuming that a variable of type A won't be accessed through a pointer
to type B. You are accessing a variable of type int through a pointer
to an incompatible type which probably isn't an issue on your
implementation unless the compiler is performing these optimizations
hence the warning appears only in this case.
Robert Gamble
.
- References:
- dereferencing type-punned pointer will break strict-aliasing rules
- From: David Mathog
- dereferencing type-punned pointer will break strict-aliasing rules
- Prev by Date: Re: swprintf declaration inconsistencies?
- Next by Date: Re: Session Adapter
- Previous by thread: dereferencing type-punned pointer will break strict-aliasing rules
- Next by thread: Re: dereferencing type-punned pointer will break strict-aliasing rules
- Index(es):
Relevant Pages
|