How to interpret binary data as an integer?
- by StackedCrooked
The codebase at work contains some code that looks roughly like this:
#define DATA_LENGTH 64
u_int32 SmartKey::SerialNumber()
{
unsigned char data[DATA_LENGTH];
// ... initialized data buffer
return *(u_int32*)data;
}
This code works correctly, but GCC gives the following warning:
warning: dereferencing pointer ‘serialNumber’ does break strict-aliasing rules
Can someone explain this warning? Is this code potentially dangerous? How can it be improved?