News for hackers. Stuff that matters.
Re:C Puzzles by FreeFull on Tuesday June 23, 2015 @08:19AM (#9764128)

    #include <stdio.h>
    int main()
    {
     float a = 12.5;
     printf("%d\n", a);
     printf("%d\n", *(int *)&a);
     return 0;
    }
This program invokes undefined behaviour when it casts float\* to int\*, so technically it could print anything, or do something else. I'm not sure how using memcpy instead would impact the puzzle's point though.
Re:C Puzzles by je-so on Tuesday June 23, 2015 @09:49AM (#9764570)
If you know that sizeof(float) >= sizeof(int) the memory alignment should be fine. Also in this case * (int* )&a does not access any bytes outside of an defined data object.

To predict the outcome one have to know the representation of float (IEEE 754) and the big/little endian issues which could be different between floats and ints.

printf("%d\n", a) does promote the value a to a double first (promotion rules) and then either the first or last part i(assuming sizeof(double) == 2*sizeof(int)) is shown depending on little or big endian architecture.

Re:C Puzzles by opcvx on Tuesday June 23, 2015 @11:58AM (#9765499)
You don't know C.

Reinterpreting an object as an incompatible object is undefined. int and float are incompatible.

C Standard also specifically states that using an incorrect printf format specifier will result in undefined behavior.

Re:C Puzzles by FreeFull on Tuesday June 23, 2015 @03:11PM (#9766981)
I have experimented with printf("%d\n", a); using gcc. When compiled for 32-bit x86, it displays the lower 32 bits of the double (which for 12.5 happen to be all 0).

On x86-64, it displays a random value each time, which actually happens to be some pointer (randomised by ASLR). This is because register passing is used on x86-64, and the the double is stored into a floating point register, but %d reads from a different register.

printf("%d\n", * (int*)&a); produces the number 1095237632 for both x86 and x86-64.

Re:C Puzzles by JoeAltmaier on Tuesday June 23, 2015 @08:21AM (#9764134)
Technically, it will print the first N bytes of the float as an int. Where N is the number of bytes ints compile to on the platform. So with knowledge of the platform, the result is entirely predictable.
Re:C Puzzles by adsche on Tuesday June 23, 2015 @08:51AM (#9764261)
Only with knowledge of platform AND compiler, though.

Also these are C puzzles and casting pointers to incompatible types and dereferencing them is undefined behavior. Using memcpy would indeed fix this according to e.g. [1].

[1] http://blog.llvm.org/2011/05/what-every-c-programmer-should-... (section "Violating Type Rules")

Re:C Puzzles by userbinator on Tuesday June 23, 2015 @10:24AM (#9764801)
UB according to the standard has no restrictions, but I think the point of these puzzles is to explain what could reasonably happen on a typical system, and why.
Re:C Puzzles by unwind on Tuesday June 23, 2015 @09:31AM (#9764475)
Actually it invokes undefined behavior when it passes a float to printf() and then tries to print it using %d (which of course expects int). Don't do that.