Fast comparison of char arrays?
<p>I'm currently working in a codebase where IPv4 addresses are represented as pointers to <code>u_int8</code>. The equality operator is implemented like this:</p>

<pre><code>bool Ipv4Address::operator==(const u_int8 * inAddress) const
{
    return (*(u_int32*) this-&gt;myBytes == *(u_int32*) inAddress);
}
</code></pre>

<p>This is probably the fasted solution, but it causes the GCC compiler warning:</p>

<pre><code>ipv4address.cpp:65: warning: dereferencing type-punned pointer will break strict-aliasing rules
</code></pre>

<p>How can I rewrite the comparison correctly without breaking strict-aliasing rules and without losing performance points?</p>

<p>I have considered using either <code>memcmp</code> or this macro:</p>

<pre><code>#define IS_EQUAL(a, b) \
    (a[0] == b[0] &amp;&amp; a[1] == b[1] &amp;&amp; a[2] == b[2] &amp;&amp; a[3] == b[3])
</code></pre>

<p>I'm thinking that the macro is the fastest solution.</p>

<p>What do you recommend?</p>

<p><b>Update</b><br>
I just read the article <a href="http://blogs.msdn.com/b/cellfish/archive/2010/02/24/squeezing-performance-out-of-memcmp-usage.aspx" rel="nofollow">Squeezing performance out of memcmp usage</a> which explains how the compiler (Visual Studio, but perhaps also GCC) can optimize <code>!memcmp(..)</code> calls.</p>
