I need to save a float value which is a copied-memory from an integer value.
In reinterpretedFloat
function, I made an sample integer and copy the memory to a float variable.
The thing is the value changes when the memcpy-ed float returns.
Here is the sample code.
#include <stdio.h>
#include <stdint.h>
void printHex(const unsigned char* buff, int count)
{
printf("0X");
for (int i = 0; i < count; ++i)
{
printf("\t%X", buff[i]);
}
printf("\n");
}
float reinterpretedFloat()
{
int32_t value = 0x7F845E58;
float tmp;
memcpy(&tmp, &value, sizeof(float));
printHex(reinterpret_cast<const unsigned char*>(&tmp), 4); //memcpy
return tmp;
}
int main()
{
float newFloat = reinterpretedFloat();
printHex(reinterpret_cast<const unsigned char*>(&newFloat), 4); //returned value
return 0;
}
This is the result.
0X 58 5E 84 7F(memcpy)
0X 58 5E C4 7F(returned value)
What I expected was 0X 58 5E 84 7F
...
Any body can explain why this is happening? In x64 configuration, this does not happens.