Open PerGraa opened 7 years ago
I always run on 64-bit and don't have an issue... but I have always wondered if the pragma pack is a good idea. The only reason I had put it there was so that the Variant will be more space optimized--but it may not be worth it. Thoughts?
Well, there are many thoughts to be had on the subject of undefined behaviour and (un)aligned access :)
In my view it comes down to portability. Should users be able to use jvar at any given architecture/compiler setup without silent undefined bombs waiting to explode? Or do you support x86-family CPUs only?
Unaligned access will typically work fine on the x86-family CPUs, but there might be a penalty in CPU cycles. So there you have a space/time trade-off to consider: Save space using #pragma pack versus possible slowdown in unaligned access?
As a counter example, unaligned access will typically make ARM MCUs crash.
In any case the above also changes over time, since Intel and ARM engineers are working to limit the implications of unaligned access. There is a lot of material available, I like this as a starting point: http://lemire.me/blog/2012/05/31/data-alignment-for-speed-myth-or-reality/
Using the UB sanitizer gives us a number of errors:
The offending code seems to be in
var.h
:If I change the pragma push argument to 8 (bytes) on my 64-bit architecture, then all is well and the sanitizer errors disappear. But this may cause trouble on non-64-bit. Also the
sizeof(std::string)
is implementation/compiler dependent, so it might be better just to remove the pragma push and pop altogether?