• 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: Object for variable array of fixed structures
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Object for variable array of fixed structures


  • Subject: Re: Object for variable array of fixed structures
  • From: Steve Sheets <email@hidden>
  • Date: Mon, 12 Jun 2006 22:28:37 -0400

A number of people have pointed out that I could use NSMutableArray to manage variable size array of NObjects. To use my example, I would store NSRect inside an NSData wrapper, or create subclass of NSObject with an NSRect field or store an NSRect inside an NSValue for each instance of NSRect I want. I would then store X number of these objects using one NSMutableArray.

Yes, this is true, and this is what I am trying to avoid.

Using the NSMutableArray approach, when I want an array with 1000 rectangles, I would be creating 1001 objects, one NSMutableArray & one thousand NSValue objects (using that method). Instead if I use NSMutableData to store allocate enough memory for an array of rectangles, I would only be working with one object, though I will give you that it is an large object.

I not sure what the over head for having a given NSValue object, but I can not imagine that 1001 objects do not take up a bit more of memory than 1 object.

Of course, memory is cheap now a days. I just can't stand storing data that inefficiently.

My structures are strictly scalar values, without reference to handles inside them (a typical example might contain 2 rectangles, a pointer, and some flags, yes, these are for graphic animations).

Any other thoughts on this technique?

Steve Sheets
Midnight Mage Software

On Jun 12, 2006, at 4:12 PM, Steve Sheets wrote:
Just another design question...

Are there any examples of good technique to implement variable size arrays of fixed structures?

For example, I want to create an array of NSRect inside my code. Obviously, I could make the data structure be a fixed length array, and not worry about objects. While this array may be small, but occasionally it might be as large as a thousand items in the array. I do not want to always allocated fixed size array all the time.

My solution to handle this involves creating a new category of methods for NSMutableData (named MyMutableArrayRect), which has calls to size the array (based on length of NSRect structure), retrieve items in the array and set items in the array. The new calls look like something like:

- (void) arrayRectSetSize:(int)p_number_item;

- (NSRect) arrayRectGetItem:(int)p_position;

- (void) arrayRectSetItem:(int)p_position
	withRect:(NSRect)p_rect;

Originally the new calls were part of a subclass, but I found using a Category gave me all I needed, while only working with NSMutableData.

Is this the correct approach? I am about to expand this idea to a couple other large fixed size structure, but using the same basic ideas.

Am I missing something and there is a better approach?

Thank you!

Steve Sheets
Midnight Mage Software






_______________________________________________ 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
  • Follow-Ups:
    • Re: Object for variable array of fixed structures
      • From: Steve Sheets <email@hidden>
    • Re: Object for variable array of fixed structures
      • From: "Michael Ash" <email@hidden>
    • Re: Object for variable array of fixed structures
      • From: Chris Suter <email@hidden>
References: 
 >Object for variable array of fixed structures (From: Steve Sheets <email@hidden>)

  • Prev by Date: Re: Apple Data Plotting Framework
  • Next by Date: Re: Object for variable array of fixed structures
  • Previous by thread: Re: Object for variable array of fixed structures
  • Next by thread: Re: Object for variable array of fixed structures
  • Index(es):
    • Date
    • Thread