• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: error in using NSNumber's numberWithInt: method not flagged
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: error in using NSNumber's numberWithInt: method not flagged


  • Subject: Re: error in using NSNumber's numberWithInt: method not flagged
  • From: John McCall <email@hidden>
  • Date: Sun, 09 Jun 2013 13:45:52 -0700

On Jun 9, 2013, at 1:32 PM, Kyle Sluder <email@hidden> wrote:
> On Jun 9, 2013, at 1:26 PM, Boyd Collier <email@hidden> 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@hidden)

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:

This email sent to email@hidden


  • Follow-Ups:
    • Re: error in using NSNumber's numberWithInt: method not flagged
      • From: Sean McBride <email@hidden>
References: 
 >error in using NSNumber's numberWithInt: method not flagged (From: Boyd Collier <email@hidden>)
 >Re: error in using NSNumber's numberWithInt: method not flagged (From: Kyle Sluder <email@hidden>)

  • Prev by Date: Re: error in using NSNumber's numberWithInt: method not flagged
  • Next by Date: Re: error in using NSNumber's numberWithInt: method not flagged
  • Previous by thread: Re: error in using NSNumber's numberWithInt: method not flagged
  • Next by thread: Re: error in using NSNumber's numberWithInt: method not flagged
  • Index(es):
    • Date
    • Thread