• 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: Calloc for Cocoa?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Calloc for Cocoa?


  • Subject: Re: Calloc for Cocoa?
  • From: Greg Weston <email@hidden>
  • Date: Mon, 30 Jun 2003 08:29:32 -0400

On Monday, June 30, 2003, at 01:00 AM, Tomas Zahradnicky wrote:

I was used to use calloc(number_of_items, sizeof(item)) to create
an array of items. Is there some similar thing in Cocoa? The approach
like this seems me quite complex:

NSArray* a = [NSMutableArray arrayWithCapacity:number_of_items];
MyItem* item = NULL;

for(i=0; i<number_of_items; i++)
[a addObject:[[MyItem alloc] init];

Of course I can add a class method to my item class that does this,
but I'm asking if there's not already a way how to do that.

Not really, and note that they're fundamentally different things. With calloc you're grabbing a single chunk of memory to be treated as an array of objects. But you can't really have an array of Objective-C objects. With the Cocoa code you've got, you're creating a container to hold object pointers and then actually stuffing newly-created objects into it.

G
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: subclass weirdness
  • Next by Date: Re: Calloc for Cocoa?
  • Previous by thread: Re: Calloc for Cocoa?
  • Next by thread: sporadic crash - help requested in finding cause
  • Index(es):
    • Date
    • Thread