strict aliasing (again)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



hello folks,

i'm going through the code and trying to fix places where a 'dereferencing
type-punned pointer...' appears.  while trying to make it conformant i am
trying to play with the code with type punning and try to see 'what if'.

for example if i have code like this:

	template <typename T>
	struct ptr {
		T * m_ptr;
		ptr (T * p) : m_ptr(p) { }

		bool operator== (int rhs) const {
			return (*(int*) &m_ptr == rhs);
		}
	};

and i am trying to find conditions under which this could produce wrong code...
well this is my question: what are these conditions? what kind of unit test
should i create?


well this one seems to be easy to fix, but i'm getting my head dizzy from this:

	struct Vector3 {
		float x, y, z;
		float & operator[] (int i) { return *(&x+i); }
	};
	struct Vector4 {
		float x, y, z, w; 
		Vector3 const & GetVector3 () const { return *(Vector3 const *) &x; }
		Vector4 (Vector3 const & v)  : x(v[0]), y(v[1]), z(v[2]), w(0) {}
	};
	 
	struct Plane { 
		Vector3 m_Normal; 
		float   m_Distance; 
	 
		Vector4 & GetVector4 () const { return *(Vector4 *) &m_Normal; }
	};

this is simply terrible, i've never seen anything like that :) and i really
don't know how should i fix this, because accesses are spread all around the
code. easy way around at least the accesses would be something like:

	struct Vector3 {
		union {
			struct {
				float x, y, z;
			};
			float arr[3];
		};
but there is apparently no such allowed construction.

getting around the conversion (Vector3 + float) == Vector4 i really don't
know: complete rewrite hangs in my mind..

i'd really appreciate any thoughts on this.

best regards,
mojmir

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux