4 ms·
Of course, casting that int * to a float * is undefined behavior…
by saagarjha 9y ago
Of course, casting that int * to a float * is undefined behavior…
- zyx321 9y agoOf course. When you first learn about 'cool hacks' like these, you'll want to use everywhere. Then you'll realize that they don't apply to certain data types in certain environments. Then you'll come to realize that the rules governing which hacks to use are too complex to remember. Then you'll want to write a program that figures it out for you. Then you'll realize that those programs already exist, and they are called compilers. So now you are back where you started. You have made no progress, but you have gained an important insight.
- TeMPOraL 9y agoAnd from that, at some point you realize that the corollary to your #2 says that such hacks do apply to certain data types in certain environments. And then you've gained a very powerful tool to be used in specific circumstances. There's a reason a lot of these potentially non-portable tricks show up in high-performance computing (particularly in videogames) and in the embedded space.
- zyx321 9y agoThat reason being that sometimes spending a 10+ hours on a tweak that gives +0.1% performance improvement is worth it. Otherwise, remember the first rule of code optimization for beginners: "Don't." EDIT: And by "they don't apply", I mean they can crash your program or even silently corrupt your data.
- kaslai 9y agoSometimes tweaks like these might only save 10-20 cycles, which in a vacuum doesn't sound like much, until you consider that before the tweak, it started at 26 cycles and is now 6 cycles, and it's called 50 million times every second, which is a savings of 100 million cycles per second. For games, this can mean a higher frame rate. For data processing, it can mean the data gets processed significantly faster. It's especially important for things like video encoders, where even a 1% savings in CPU time can translate directly to 1% more potential revenue for the same operating cost at a company that relies heavily on video encoding. Yeah, saving those cycles doesn't really mean anything to a beginner, but they can be a huge savings for high performance computation applications.
- ithkuil 9y agoYep the correct way is: union { int i; float f;} u; u.i=*i; Now u.f contains the same bit pattern as the integer pointed to by "i" but interpreted as a float. Compilers know how to generate code efficiently out of this. Iirc this is implementation defined behavior, e.g. things like endianness alignment or padding, but it's not undefined behavior, whose evil effects can bubble up in unexpected parts of the program that apparently don't even touch the bad cast.
- saagarjha 9y agoNope, it's undefined: > Accessing an object by means of any other lvalue expression (other than unsigned char) is undefined behavior https://wiki.sei.cmu.edu/confluence/display/c/EXP39-C.+Do+not+access+a+variable+through+a+pointer+of+an+incompatible+type https://wiki.sei.cmu.edu/confluence/display/c/EXP39-C.+Do+no...
- ithkuil 9y agoI don't understand your comment. The link you pasted explicitly mentions the union example as a "compliant solution". That said, memcpy and a compiler that understand the memcpy intrinsic is probably safer; that said I have a vague memory of hitting an embedded compiler where memcpy wasn't an intrinsic
- TeMPOraL 9y agoWow, never realized the pointer cast version is undefined behaviour in C. Is C++ equivalent via reinterpret_cast undefined behaviour too? Back in my C++ days, I would much prefer pointer-cast to union-struct, because the latter was subject to alignment/padding issues you mention, while for the former, I know of no desktop architecture that could mess up a int* -> float* cast. I'm also doubly surprised because being able to do that stuff is what I'd consider the selling point of C/C++ over higher-level languages.
- saagarjha 9y agoreinterpret_cast in C++ has essentially the same rules, with exceptions made for const qualifiers and such, as well as void , char , and std::byte *. Use a memcpy instead.