Skip to content

Instantly share code, notes, and snippets.

@Kjuly
Last active December 16, 2015 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kjuly/5378119 to your computer and use it in GitHub Desktop.
Save Kjuly/5378119 to your computer and use it in GitHub Desktop.
Create a C array of retained pointers under ARC (Obj-C)
// Note calloc() to get zero-filled memory.
__strong SomeClass **dynamicArray = (__strong SomeClass **)calloc(sizeof(SomeClass *), entries);
for (int i = 0; i < entries; ++i)
dynamicArray[i] = [[SomeClass alloc] init];
// When you're done, set each entry to nil to tell ARC to release the object.
for (int i = 0; i < entries; ++i)
dynamicArray[i] = nil;
free(dynamicArray);
/*
There are a number of aspects to note:
You will need to write __strong SomeClass ** in some cases, because the default is __autoreleasing SomeClass **.
The allocated memory must be zero-filled.
You must set each element to nil before freeing the array (memset or bzero will not work).
You should avoid memcpy or realloc.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment