I tested this expression in gdb:
(gdb) p (int)[@"-1" floatValue]
$2 = 1
but
(gdb) p (int)((float)[@"-1" floatValue])
$7 = -1
comes out as I expect
Why does the first expression not return -1? Also, what is the return type of [@"-1" floatValue]?
gdb doesn't know the return type of methods (or functions):
(gdb) p [@"-1" floatValue]
Unable to call function "objc_msgSend" at 0x155d08c: no return type information available.
To call this function anyway, you can cast the return type explicitly (e.g. 'print (float) fabs (3.0)')
When you cast the expression to int, gdb assumes that the method returns an int. So it knows it can use objc_msgSend to send the message and treat the return value from objc_msgSend as an int.
When you cast the expression to a float, gdb assumes that the method returns a float. So it knows that it should use objc_msgSend_fpret to send the message and treat the return value as a float.
This is important because:
On the i386 platform, the ABI for functions returning a floating-point value is incompatible with that for functions returning an integral type. On the i386 platform, therefore, you must use objc_msgSend_fpret for functions that for functions [sic] returning non-integral type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With