Why do you need to "alloc" for initWithCString and but doesn't for stringWithFormat ?

Joined
Oct 7, 2009
Messages
14
Reaction score
0
Points
1
Location
Singapore
Why is it that these two are correct:
Code:
NSMutableString* CombinedText = [NSMutableString stringWithFormat:@"%d %d %d",1,2,3];

and
Code:
NSMutableString* CombinedText = [[NSMutableString alloc] initWithCString:"String Data" ];

The question is, why is it that you have to "alloc" when you are using the "initWithCString" function yet doesn't have to when you do stringWithFormat, I am puzzled because they BOTH returns a NSMutableString, so shouldn't they have similar initialization protocols ?
Yet when I try to:
Code:
NSMutableString* CombinedText = [ [NSMutableString alloc] stringWithFormat:@"%d %d %d",1,2,3];
It crashes ?
Likewise if I were to remove "alloc" from initWithCString.
 
Joined
Dec 13, 2007
Messages
256
Reaction score
10
Points
18
Location
United States of America
Your Mac's Specs
2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09
Any time you initialize an object with [[ClassName alloc] init], you are explicitly allocating memory for it. When you use convenience methods, such as NSString's stringWithString: and string methods, the object is already allocated and initialized in the method and the result is returned.

The difference is that, by convention, you are responsible for releasing objects that you explicitly allocate with alloc and init. So, for every alloc there should be a release somewhere later in the code. Otherwise, you are leaking the memory used to store the object. When you use convenience methods that do not contain the words copy or new, you are typically not responsible for releasing them; they have been autoreleased before being returned for your use.

This is one of the most prevalent concepts that you'll need to understand in Objective-C and Cocoa programming. I highly recommend reading Apple's Memory Management Programming Guide for Cocoa.
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
In the first one, you are using an NSString template that will allocate the space needed to return an NSString type for what you are creating.

In the second one, you are converting a C string which has different memory requirements, so you have to first allocated memory for an NSString type.

Note also that initWithCString is deprecated. See the NSString Class reference.

Lastly, the crasher occurs because you are trying to use a class method with an object instance. When you see the documentation mentioned above, you'll notice class methods have a plus (+) sign in front of them while an instance method has a minus (-) sign in front of them. A class method can only be used with the class name while a instance method works with instance variables you have have allocated.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top