• 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: NSMutableArray & Pointers
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSMutableArray & Pointers


  • Subject: Re: NSMutableArray & Pointers
  • From: "M. Uli Kusterer" <email@hidden>
  • Date: Fri, 22 Oct 2004 03:39:13 +0200

At 1:48 Uhr +0200 22.10.2004, Michael Becker wrote:
Hm, I think I just assumed it would. I thought a pointer that does not point to any object is a nil-Pointer. However, it seems as though it just keeps on pointing to the same address, giving to you whatever there is...

Just for the archives: A NIL-pointer is a pointer that doesn't point to an object, but the revere isn't necessarily true. Not every pointer that doesn't point to an object is a NIL pointer. In C, (and by extension, in ObjC and C++) a variable can hold a "stale pointer", which is a pointer to an object that has just been disposed.

Since in C, a pointer is simply a number, and the compiler doesn't retain information about it being a pointer, there is no way for the app to know at runtime that the object being pointed to has just gone away. That's why we have reference-counting in ObjC.

Each object that needs to have access to another can thus simply retain it.

If you want anything more/different, you'll have to write it yourself. A popular approach (depending on the number of objects you expect to have, of course), is to have a "master list", and then to e.g. check whether your object in the shopping cart is still contained in the master list.

If you have lots of products and few "shopping carts" for example, it may be faster to go through all shopping carts when an object is deleted from the master list and remove any copies of the objects with a placeholder object that tells your users what the item was, but doesn't let them order it anymore.

Or another option may be to save array indexes into the master list instead of pointers to objects, and then change the semantics of deleting into "replace the object in the master list with a placeholder". The net effect will be the same as above, but deletion of objects will be a lot faster, accessing objects will still be constant-time.

Of course, the downside would be that your array of products will never become smaller, because it'll be full of placeholder objects. But it would probably be possible to create an array class that efficiently handles this.

--
Cheers,
M. Uli Kusterer
------------------------------------------------------------
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden
References: 
 >NSMutableArray & Pointers (From: Michael Becker <email@hidden>)
 >Re: NSMutableArray & Pointers (From: stephane sudre <email@hidden>)
 >Re: NSMutableArray & Pointers (From: Michael Becker <email@hidden>)

  • Prev by Date: Re: Bug in NSArrayController? (immutable instead of mutable dictionaries)
  • Next by Date: Re: How do you create a NSTabViewItem containing NSImageView programmatically? I almost got it...
  • Previous by thread: Re: NSMutableArray & Pointers
  • Next by thread: Re: NSMutableArray & Pointers
  • Index(es):
    • Date
    • Thread