New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

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


Post Reply New Thread Subscribe

 
Thread Tools
Bracer

 
Bracer's Avatar
 
Member Since: Oct 07, 2009
Location: Singapore
Posts: 14
Bracer is on a distinguished road

Bracer is offline
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.
QUOTE Thanks
nabl

 
nabl's Avatar
 
Member Since: Dec 13, 2007
Location: United States of America
Posts: 256
nabl has a spectacular aura about
Mac Specs: 2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork 09

nabl is offline
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.
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
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.

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks

Post Reply New Thread Subscribe


« Why is it that instance variables with the same name are allowed ? | macbook mysql error »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

All times are GMT -4. The time now is 11:33 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?