0

Possible Duplicate:
NSString allocation and initializing

I was wondering why some objects don't need to be initialized and have memory allocated. I read this: Why do some objects not need to be initialized before use in objective-c? And they said that the method date initialized and allocated memory for today. But if I just wrote NSString *str = @"Hello"; does it still initialized and allocated?

Community
  • 1
  • 1
stumped
  • 3,235
  • 7
  • 43
  • 76

1 Answers1

5

When the compiler sees @"Hello", it sticks a symbol in the .o file that says "hey, dev wants a string with contents "Hello". When the linker links together everything, it unique-ifies all the strings and emits a string table that contains all the strings that the code defined within.

So, in effect, the compiler is "allocating and initializing" the string instances at compile and link time.

They are actually stored in a special format that a particular subclass of the NSString class cluster can encapsulate. When your app is run, the instances of this subclass already exist and are simply mapped into memory.

So, no, not allocated and initialized. But, yes, still objects in every sense of the word. The only caveat is that they ignore retain/release/autorelease and, thus, when you do NSString* foo = @"foo";, technically that should also be retained. But, by convention, no one ever bothers and that's just fine.

bbum
  • 162,346
  • 23
  • 271
  • 359