Quantcast
Channel: User Marco A. - Stack Overflow
Viewing all articles
Browse latest Browse all 45

Answer by Marco A. for Casting char[] to usigned int gives: dereferencing type-punned pointer will break strict-aliasing rules

$
0
0

The problem with your code is that violates strict aliasing rules and thus it's potentially unsafe.

You can either hide the warning with -Wno-strict-aliasing (this won't solve your problem), modify your data structure or avoid the problem entirely by specifying position and length of your binary copy as Matt suggested (probably the best option):

unsigned int msgLength; memcpy(&msgLength, m_recvBuf, sizeof(msgLength)); msgLength = ntohl(msgLength);

Notice: I've not been getting the error with clang 3.4 and gcc 4.8.2 in -O3, that means the compiler might have optimized the warning away. Anyway that doesn't assure you your code is safe.


Viewing all articles
Browse latest Browse all 45

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>