• 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: Very interesting ordering caveat with NSArray arrayWithObjects:
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Very interesting ordering caveat with NSArray arrayWithObjects:


  • Subject: Re: Very interesting ordering caveat with NSArray arrayWithObjects:
  • From: Eric Hermanson <email@hidden>
  • Date: Sat, 04 Apr 2009 02:14:37 -0400

A comma is a sequence yet the order in arrayWithObjects is indeterminate. It must be the var arg causing the ordering mix.

Sent from my iPhone

On Apr 4, 2009, at 12:29 AM, Michael Ash <email@hidden> wrote:

On Fri, Apr 3, 2009 at 8:30 PM, Eric Hermanson <email@hidden> wrote:
Some (or most) people might be aware of this caveat, but I was not, so I'll
share it.


Consider this code:

NSArray *array = [NSArray arrayWithObjects:[MyCounterClass newObject],
[MyCounterClass newObject], nil];


where [MyCounterClass newObject] is a static method that returns a new
autoreleased instance that simply stores an incrementing int32 counter in
its instance variable, e.g.


   self.oid = SomeStaticCounter++;   // (or ideally,
OSAtomicIncrement32Barrier(&SomeStaticCounter);

Now, one would expect that the array would contain:

       element 1: MyCounterInstance.oid=1
       element 2: MyCounterInstance.oid=2

However, this is NOT the case. Either the compiler or the runtime executes
the SECOND call to [MyCounterClass newObject] FIRST, so the array actually
contains:


       element 1: MyCounterInstance.oid=2
       element 2: MyCounterInstance.oid=1

NSArray arrayWithObjects: is of course correctly putting the objects into
the array in the correct natural ordering, but the objects are CREATED on
the stack in the oppose order. Maybe most people knew that, I did not. So
the (or a) workaround is:


MyCounterClass *object1 = [MyCounterClass newObject];
MyCounterClass *object2 = [MyCounterClass newObject];
NSArray *array = [NSArray arrayWithObjects: object1, object2, nil];

This is actually a "feature" of C, which ObjC inherits. C does not define an order of operations except across "sequence points", which are basically semicolons, although C defines some others too. Different parts of a statement are executed in an arbitrary order. Basically, the compiler can decide which order suits it best. As such, conforming C (and thus ObjC) code must never rely on the order of execution of function arguments, arithmetic subexpressions, or anything else of that nature. In any given statement, there should never be two parts of the statement that have interdependent side effects.

Wikipedia has a decent discussion of this concept along with some
illuminating examples:

http://en.wikipedia.org/wiki/Sequence_point

Mike
_______________________________________________

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
_______________________________________________

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: Very interesting ordering caveat with NSArray arrayWithObjects:
      • From: Jean-Daniel Dupas <email@hidden>
    • Re: Very interesting ordering caveat with NSArray arrayWithObjects:
      • From: Michael Ash <email@hidden>
References: 
 >Very interesting ordering caveat with NSArray arrayWithObjects: (From: Eric Hermanson <email@hidden>)
 >Re: Very interesting ordering caveat with NSArray arrayWithObjects: (From: Michael Ash <email@hidden>)

  • Prev by Date: How can I get data from a javascript function in a WebView into the enclosing Cocoa App?
  • Next by Date: Re: Error: mutating method sent to immutable object
  • Previous by thread: Re: Very interesting ordering caveat with NSArray arrayWithObjects:
  • Next by thread: Re: Very interesting ordering caveat with NSArray arrayWithObjects:
  • Index(es):
    • Date
    • Thread