How do you write the float value ?
I already work on a project that handle multiple object with different data size. We want to keep the object properties in a chain list but stored data in a single common big buffer. So in this buffer we have 8-bit signed integer, followed by a 32-bit float, followed by an unsigned integer 64 bit, etc ... obviously except the first element no one was aligned to save space. So when we want to write a value we do :
// Write float value to any position in your big buffer
float fValueToWrite = 10;
memcpy(ptr + 12, &fValueToWrite, sizeof(fValueToWrite));
When we want to read the value we do :
// Read the float from any unalign position of your big buffer
float fReadValue = 0;
memcpy(&fReadValue, ptr + 12, sizeof(fReadValue));
The trick is using memcpy that don't care about reading from a non word boundary.
But if we did this to read :
float buffer[256] = {0};`
uint8_t* ptr = (uint8_t *)buffer;
float fCrashReadValue = *((float *)(ptr + 11));
may cause alignment fault on processor that do not support reading from boundary divisible by 11 (LoL indeed it's a prime number). Thus, if your CPU is 32 bits in this case 12 is a valid boundary as long as ptr
is pointing to the allocated buffer and not on another pointer that may not be properly aligned.
By experience I can told your that many ARM processor that I worked with will fault using a non-boundary address. But x86 (intel) processor will quietly realign with a penalty performance.
Hope it will help you :)