N
Noob
Hello,
I'm supposed to "clean up" code that does things which the
standard frowns upon, such as
typedef long int LONG;
typedef unsigned long ULONG;
{
unsigned char csw[ 80 ] = { 0 };
fill_array(csw);
LONG sign = *(LONG*)&csw[0];
ULONG tag = *(ULONG *)&csw[4];
LONG residue = *(LONG*)&csw[8];
}
AFAIU, there are several problems with this code.
1. Since csw is a byte array, it might not be correctly
aligned for long accesses.
2. I think the compiler is allowed to assume that a given
object cannot be accessed through two incompatible pointers
(Is this what aliasing refers to?)
Strangely, my compiler (gcc) complains only about the
first dereference:
warning: dereferencing type-punned pointer will break strict-aliasing rules
AFAICT, all three lines have the same issue, right?
It seems the compiler should complain "equally" about the
three dereferences. Do you agree?
As for the fix, I think all is needed is, e.g.
LONG sign;
memcpy(&sign, csw+0, sizeof sign);
/* etc */
Do you agree?
Regards.
I'm supposed to "clean up" code that does things which the
standard frowns upon, such as
typedef long int LONG;
typedef unsigned long ULONG;
{
unsigned char csw[ 80 ] = { 0 };
fill_array(csw);
LONG sign = *(LONG*)&csw[0];
ULONG tag = *(ULONG *)&csw[4];
LONG residue = *(LONG*)&csw[8];
}
AFAIU, there are several problems with this code.
1. Since csw is a byte array, it might not be correctly
aligned for long accesses.
2. I think the compiler is allowed to assume that a given
object cannot be accessed through two incompatible pointers
(Is this what aliasing refers to?)
Strangely, my compiler (gcc) complains only about the
first dereference:
warning: dereferencing type-punned pointer will break strict-aliasing rules
AFAICT, all three lines have the same issue, right?
It seems the compiler should complain "equally" about the
three dereferences. Do you agree?
As for the fix, I think all is needed is, e.g.
LONG sign;
memcpy(&sign, csw+0, sizeof sign);
/* etc */
Do you agree?
Regards.