Alias to a double array from another byte array

  alias, arrays, c++, type-alias

I am trying to create an alias to a double array from an array of bytes. But it happens to be behaving differently than I have expected.
The array of bytes stores – sensor code (int) followed by location data (double[3]). This is what I have tried

std::byte mSensorData[sizeof(int32_t) + 3 * sizeof(double)];
int32_t&  mSensorCode   = *((int32_t*)     &mSensorData);
double (& mLocation)[3] = *((double (*)[3])&mSensorData + sizeof(int32_t));

/* some code, byte* gpsData contains location data */
memcpy(mLocation, gpsData, sizeof(mLocation));

DEBUG(std::cout<<mSensorCode  <<std::endl;)
DEBUG(std::cout<<mLocation[0] <<std::endl;)
DEBUG(std::cout<<mLocation[1] <<std::endl;)
DEBUG(std::cout<<mLocation[2] <<std::endl;)

DEBUG(std::cout<<*((int*)&mSensorData[0])     <<std::endl;)
DEBUG(std::cout<<*((double*)&mSensorData[4])  <<std::endl;)
DEBUG(std::cout<<*((double*)&mSensorData[12]) <<std::endl;)
DEBUG(std::cout<<*((double*)&mSensorData[20]) <<std::endl;)

Result I got:

120
37.422
-122.084
16.6551
120
0
0
0

Source: Windows Questions C++

LEAVE A COMMENT