D
David Mathog
We've established that a line of code like:
(void) function( (long *) &(something) );
may (and probably should) generate one of these
warning: dereferencing type-punned pointer will break
strict-aliasing rules
if "something" is of some other type, say "int".
Assume for the sake of argument that elsewhere in the program we have
established that sizeof(int)==sizeof(long), and so we know it is safe
in terms of bytes of storage to keep both int and long in "something".
Then this variant would eliminate the type-punned pointer warning:
int something[SIZE];
int itmp,i;
long ltmp;
/* set i, other code, then */
(void) function( <mp); /* expects (long *) argument */
something=(int) ltmp;
I'm guessing that if the temporary variable was not used anywhere else,
then the compiler would usually be smart enough to eliminate it and to
generate code pretty much equivalent to the one that generates the
type punned warning, but it would not generate such a warning because
here the compiler knows what it can get away with to store a long into
the int array.
The question is, is there some other way, within the C language
standard, to inform the compiler which sorts of pointers are equivalent,
so that function() could store directly into something without having
to pass it through a temporary variable?
Regards,
David Mathog
(void) function( (long *) &(something) );
may (and probably should) generate one of these
warning: dereferencing type-punned pointer will break
strict-aliasing rules
if "something" is of some other type, say "int".
Assume for the sake of argument that elsewhere in the program we have
established that sizeof(int)==sizeof(long), and so we know it is safe
in terms of bytes of storage to keep both int and long in "something".
Then this variant would eliminate the type-punned pointer warning:
int something[SIZE];
int itmp,i;
long ltmp;
/* set i, other code, then */
(void) function( <mp); /* expects (long *) argument */
something=(int) ltmp;
I'm guessing that if the temporary variable was not used anywhere else,
then the compiler would usually be smart enough to eliminate it and to
generate code pretty much equivalent to the one that generates the
type punned warning, but it would not generate such a warning because
here the compiler knows what it can get away with to store a long into
the int array.
The question is, is there some other way, within the C language
standard, to inform the compiler which sorts of pointers are equivalent,
so that function() could store directly into something without having
to pass it through a temporary variable?
Regards,
David Mathog