It is currently Thu Mar 28, 2024 7:17 pm

Mulle kybernetiK Optimization

Forum for Optimizations around Cocoa and Mac OS X

C-Style Arrays of Objective-C objects

Optimizations concerning <i>Foundation</i> or <i>Objective-C</i> and its runtime
 
Posts: 1
Joined: Sun Sep 05, 2004 9:36 pm
Location: Sunnyvale, CA
Website: http://cocoadevcentral.com/

C-Style Arrays of Objective-C objects

Post by scottstevenson » Sun Sep 05, 2004 9:55 pm

A simple one, but I think sometimes people forget that you're free to use c-style arrays of Objective-C objects where you need ever last ounce of speed:

Code: Select all

// choose arbitrary size for array

int count = 20;

// allocate memory for array of pointers

NSString ** stringArray = (NSString **)malloc ( sizeof ( NSString *) * count );

// create objects and log them to the console

for (i = 0; i < count; i++)
{
    stringArray[i] = [[NSString alloc] initWithFormat:@"String %d",(i+1)];
    NSLog(@"stringArray[%d]: %@", i, stringArray[i]);
}

// release objects

NSString * theString;
for (i = 0; i < count; i++)
{
    theString = stringArray[i];
    [theString release];
}   

// free malloc'd memory

free (stringArray);




Also worth looking at NSArray's -getObjects:

- Scott

Return to “Foundation & Objective-C”

Who is online

Users browsing this forum: No registered users and 1 guest