Overview
Features
Download
Documentation
Community
Add-Ons & Services

Portable way to enforce data alignment

General discussion regarding the development of POCO for contributors.

Portable way to enforce data alignment

Postby alex » 22 Jan 2007, 19:44

Is there such a thing? I am accustomed to using '#pragma pack' on windows, but that is implmentation dependent. Is there a portable way to ensure tight packing of data-only structs?
alex
 
Posts: 1158
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Portable way to enforce data alignment

Postby guenter » 22 Jan 2007, 20:01

As far as I know, no.
For portable code, packing is rather problematic, because some architectures do not like misaligned data at all. In the best case, on this platforms, you'll experience a huge performance penalty for a misaligned access. In the worst case, your program will crash. If you need portable data alignment, the best thing is to align all data at boundaries corresponding to their byte size. E.g., put every 16-bit integer on an address where addr mod 2 == 0, every 32-bit integer on an address where addr mod 4 == 0, etc.
guenter
 
Posts: 1165
Joined: 11 Jul 2006, 16:27
Location: Austria


Return to Contributors

Who is online

Users browsing this forum: No registered users and 2 guests