How to interpret binary data as an integer?

Posted by StackedCrooked on Stack Overflow See other posts from Stack Overflow or by StackedCrooked
Published on 2010-05-27T15:23:51Z Indexed on 2010/05/27 15:31 UTC
Read the original article Hit count: 145

Filed under:

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?

© Stack Overflow or respective owner

Related posts about c++