Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to use "classic" malloc()/free() in Objective-C/iPhone apps?

I've been playing around with iPhone development for a while, and although it feels a bit awkward when you're a "hard core" .NET developer, it's not all that bad once you get used to it.

In every book I read about Objective-C, there's only talk about retain/release (reference counting) for memory management. As an old-skool C/C++ developer, it seems strange that allocating the "normal" way, using malloc() and free() is only mentioned in some footnotes.

I know that malloc() and free() work in Objective-C, but I'm curious if it is common practice or not. After all, if I want to allocate an array of 100 integers, it seems that this is the most efficient way to do it:

int *array = malloc(sizeof(int) * 100);  memset(array,0,sizeof(int) * 100);  // use the array  free(array); 

Is this indeed the best way, or should I avoid plain C memory management?

like image 209
Philippe Leybaert Avatar asked Jul 19 '09 19:07

Philippe Leybaert


People also ask

When should I use free and malloc?

To allocate a memory block you use malloc() To reallocate a memory block with specific size you use realloc() To de-allocate previously allocated memory you use free()

What is malloc in IOS?

The function for allocating any necessary storage.

Can you malloc after free?

Occasionally, free can actually return memory to the operating system and make the process smaller. Usually, all it can do is allow a later call to malloc to reuse the space. In the meantime, the space remains in your program as part of a free-list used internally by malloc .

What does free malloc do?

One implementation of malloc/free does the following: Get a block of memory from the OS through sbrk() (Unix call). Create a header and a footer around that block of memory with some information such as size, permissions, and where the next and previous block are.


2 Answers

There's an Objective-C wrapper around raw memory which I like to use a lot for similar tasks: NSMutableData. It has the benefit of giving you retain/release ownership plus it can grow the array easily (without having you to do the realloc yourself).

Your code would look like:

NSMutableData* data = [NSMutableData dataWithLength:sizeof(int) * 100]; int* array = [data mutableBytes]; // memory is already zeroed  // use the array  // decide later that we need more space: [data setLength:sizeof(int) * 200]; array = [data mutableBytes]; // re-fetch pointer in case memory needed to be copied  // no need to free // (it's done when the autoreleased object is deallocated) 
like image 197
Nikolai Ruhe Avatar answered Sep 17 '22 12:09

Nikolai Ruhe


It's perfectly fine -- Objective-C is a strict superset of C, so if you want to write plain C, there's nothing preventing you from doing so. In many cases, it's advantageous to use malloc and free to avoid the overhead of the Objective-C runtime.

For example, if you need to dynamically allocate an array of an unknown number of integers, it's often simpler and easier:

int *array = malloc(N * sizeof(int));  // check for NULL return value! // use array[0]..array[N-1] ... free(array); 

Versus:

NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:N]; // use NSMutableArray methods to do stuff with array; must use NSNumbers instead // of plain ints, which adds more overhead ... [array release]; 

I was working on a word game for the iPhone, and we had to load a multi-megabyte dictionary of valid words. The word list was loaded into one giant char array allocated with malloc(), with some clever optimizations to reduce the memory size even more. Obviously for something like this, the overhead of using an NSArray is completely impractical on the limited iPhone. I don't know exactly what the overhead is, but it's certainly more than one byte per character.

like image 39
Adam Rosenfield Avatar answered Sep 17 '22 12:09

Adam Rosenfield