Kristen Martinson
Kristen Martinson

Reputation: 1869

NSObject: on the heap or the stack?

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

Upvotes: 4

Views: 370

Answers (3)

kennytm
kennytm

Reputation: 523224

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.

Upvotes: 7

iandotkelly
iandotkelly

Reputation: 9124

Mmm, aren't all objective-c objects dynamically allocated on the heap?

NSString has one exception to this rule - declaring them like @"This is a string", they are not placed on the heap.

Upvotes: 2

Firoze Lafeer
Firoze Lafeer

Reputation: 17143

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.

Upvotes: 4

Related Questions