Jump to content
  • Advertisement
Sign in to follow this  
irreversible

Portability: am I guaranteed for union size/base address to be the same on various compilers for different OS's?

This topic is 1497 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm developing for Windows in VS and the following works very much as expected: the union is 64 bytes and all members have the same base address, which is at offset 0. This enables me to do some cheap voodoo by using the base address to directly copy contents without caring about the type. I'm not really planning anything specific at this time, but I'm at least trying to keep portability in the back of my mind. In any case, I can't immediately find this in the C++ standard and this seems like a potential caveat.

 

In terms of portability my targets are, at one point, Linux and MacOS.

 

void* ptr = &value;

	union Value {
		struct { float _mat4[16]; };
		struct { float _mat3[9]; };
		struct { float _mat2[4]; };
		struct { float _float; };
		struct { float _vec4[4]; };
		struct { float _vec3[3]; };
		struct { float _vec2[2]; };
		struct { int _int; };
		struct { int _ivec2[2]; };
		struct { int _ivec3[3]; };
		struct { int _ivec4[4]; };
		struct { unsigned int _uint; };
		struct { unsigned int _uvec2[2]; };
		struct { unsigned int _uvec3[3]; };
		struct { unsigned int _uvec4[4]; };
		struct { int _bool; };
		struct { int _bvec2[2]; };
		struct { int _bvec3[3]; };
		struct { int _bvec4[4]; };
		struct { int _smp; };
		} value;

Share this post


Link to post
Share on other sites
Advertisement

An int most not be 32-bit on all platforms, therefor I would recomment to either use fixed length types or to define your own type. Here's an example of stdint using fixed length ints.

Share this post


Link to post
Share on other sites

Thanks for the replies, guys! Frankly, since I've been on a single OS with a single compiler I haven't yet got around to rolling my own basic types.

 

EDIT: @Bacterius - to elaborate a bit: the value field is essentially a dumping ground for raw data. I memcpy() stuff in for temporary storage in my shader code and set a dirty flag. When the app generates a draw call and a value has changed, it gets passed on to the driver. So yes, this isn't much more than a blob style "variant of a type" - in fact, all it ever needs to have is the size equivalent of OpenGL's mat4. Though the int size is certainly something to look out for.

Edited by irreversible

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!