NSString memory allocation

Working through Chapter 3 of Cocoa Programming for Mac OS X. Question: why can you do this

NSString *temp = @"this is a string";

and you don’t have to do this:

NSString *temp = [[NSString alloc] init];
temp = @"this is a string";

One thought on “NSString memory allocation

  1. I found the answer to my own question. On page 405 of Stephen G. Kochan’s Programming in Objective-C, he writes, “Space for constant strings is allocated differently in memory than other objects. They have no reference counting mechanism because they can never be released.” An instance of NSConstantString is created.

Leave a Reply

Your email address will not be published. Required fields are marked *

*