Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSObject: on the heap or the stack?

Is an Objective-C object, e.g., NSString, placed on the stack or the heap?

like image 740
Kristen Martinson Avatar asked Dec 13 '22 11:12

Kristen Martinson


2 Answers

The pointee of an Objective-C object is stored on the heap. But there are two exceptions: constant strings like @"foo" and block literals are stored in the __DATA segment (for global variables).

Normally none of ObjC objects will be stored on the stack.

like image 56
kennytm Avatar answered Jan 02 '23 20:01

kennytm


They are allocated on the heap. That's true for basically all Objective-C objects, blocks being the only exception I can think of at the moment.

like image 32
Firoze Lafeer Avatar answered Jan 02 '23 20:01

Firoze Lafeer