Monday, December 28, 2015

Where are objects and variables stored in Objective-C (Heap or Stack)?

In Objective-C, objects are usually created on the heap:

    NSObject *obj = [[NSObject alloc] init];

The storage for the obj variable itself is on the stack, but the object it points to is in the heap. The [NSObject alloc] call allocates a chunk of heap memory, and fills it out to match the layout needed for an NSObject.

A stack object is just an object where the memory for that object is allocated on the stack. Objective-C doesn't have any support for this directly

Objective-C supports stack objects only for Blocks.