On Wednesday, July 21, 2010 4:55 PM, Andrew Haley wrote:
This is going to be hard. Your library is based on the assumption that
struct point
{
double x, y;
};
and
double pts[2];
have exactly the same memory layout, and that there is no padding, and
that pointers to them can be aliased. Your question is more or less
equivalent to asking how to disable strict aliasing without
-fno-strict-aliasing. I can't think of any way to solve this other
than fixing the code. There's no legal way that you can cast a
pointer to a scalar to a pointer to some struct type.
Andrew,
Thank you for your response and explanation of the problem. I think you are
most likely right: the only (best) way is to fix the code. Unfortunately,
this was just one example of many throughout the library. It seems this was
a favourite trick in its development!
I have found that changing ...
double pts[8];
point* points = (point*)pts;
points[0] = function1(points+1, 3);
function2(pts, 8);
... to ...
char pts[8 * sizeof(double)] __attribute__ ((aligned (sizeof(double))));
point* points = (point*)pts;
points[0] = function1(points+1, 3);
function2((double*)pts, 8);
... while almost certainly worse than the current situation, does at least
silence the error in this instance. I can then run tests on the library to
demonstrate its correct operation. This may not be the preferred way of
dealing with this, but the shear quantity of code that may become broken,
causes me to hesitate!
Or, do you think I'm heading for bigger trouble down this route?
Thanks for your help!
Andy