word_baby
word_baby

Reputation: 57

Why does (int)[@"-1" floatValue] does not return -1

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]?

Upvotes: 3

Views: 178

Answers (1)

rob mayoff
rob mayoff

Reputation: 385600

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.

Upvotes: 6

Related Questions