On Jun 9, 2013, at 1:32 PM, Kyle Sluder <[email protected]> wrote:
> On Jun 9, 2013, at 1:26 PM, Boyd Collier <[email protected]> wrote:
>> Quite by chance, I stumbled on to what seems to be an error that ought to be 
>> caught by the compiler but wasn't.  Here's an example:
>> 
>>   NSNumber *testNumber = [NSNumber numberWithInt:5];
>>   int testInt = (int)testNumber;
>> 
>> […snip…]
>> 
>> Should I file a bug report or is this a well-known anomaly that I've just 
>> come across?
> 
> Sitting on a runway right now, so I can't double-check the C spec, but I 
> believe this is a consequence of C's use of integers for truth types.
> 
> A pointer must be convertible to integer type, because it can be used in the 
> form `if (expressionOfPointerType)`. It follows that casting to int must also 
> be allowed. I'm not sure what the spec says about the actual value of the 
> result.


Explicitly converting a pointer to an integer type (other than _Bool/bool) 
reinterprets the bit-representation of the pointer as an integer.  There are a 
lot of low-level tricks you can then play with that.  Of course, you need to 
use an integral type that's large enough to store the pointer value.

Playing with the bit-representation of Objective-C pointers specifically is 
ill-advised because the implementation reserves the right to play its own games 
with object pointers (such as the optimized representation of integer values 
when boxed as NSNumbers on 64-bit platforms), but nonetheless this behavior is 
inherited from C and cannot really be changed.

John.
_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to