How to interpret binary data as an integer?
<p>The codebase at work contains some code that looks roughly like this:</p>

<pre><code>#define DATA_LENGTH 64

u_int32 SmartKey::SerialNumber()
{
    unsigned char data[DATA_LENGTH];
    // ... initialized data buffer
    return *(u_int32*)data;
}
</code></pre>

<p>This code works correctly, but GCC gives the following warning:</p>

<pre><code>warning: dereferencing pointer ‘serialNumber’ does break strict-aliasing rules
</code></pre>

<p>Can someone explain this warning? Is this code potentially dangerous? How can it be improved?</p>

<p><b>Update</b><br>
With thanks to James McNellis' answer I came up with the following utility function:</p>

<pre><code>template&lt;class T, class Data&gt;
T BinaryCast(const Data &amp; inData)
{
    T ret;
    std::copy(&amp;inData[0], &amp;inData[0] + sizeof(ret), reinterpret_cast&lt;char*&gt;(&amp;ret));
    return ret;
}

u_int32 SmartKey::SerialNumber()
{
    unsigned char data[DATA_LENGTH];
    // ... initialized data buffer
    return BinaryCast&lt;u_int32&gt;(data);
}
</code></pre>

<p>Feel free to suggest improvements!</p>
