Re: Calloc for Cocoa?
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.