I am surprised that no one has proposed this yet for a solution that is safe and correct alignment in all . (well, any architecture where there are 8 bits per byte).
inline int16_t ReadINT16(uint8_t *ByteArray, int32_t Offset) { int16_t result; memcpy(&result, ByteArray+Offset, sizeof(int16_t)); return result; };
And I believe that the overhead of memcpy could be avoided:
inline int16_t ReadINT16(uint8_t *ByteArray, int32_t Offset) { int16_t result; uint8_t* ptr1=(uint8_t*)&result; uint8_t* ptr2 = ptr1+1; *ptr1 = *ByteArray; *ptr2 = *(ByteArray+1); return result; };
I believe alignment issues do not throw x86 exceptions. And if I remember, Windows (when it worked on Dec Alpha and others), the trap of eliminating alignment and correction (with a modest performance). And I remember how I found out that Sparc on SunOS just crashed when you had alignment problems.
source share