Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zippyfly

macrumors regular
Original poster
Mar 22, 2008
141
0
Hello. Why is it:

Code:
@synthesize statusText;

- (IBAction) buttonPressed: (id) sender {
	
	NSString *title = [sender titleForState:UIControlStateNormal];
	NSString *newText = [[NSString alloc] initWithFormat: @"%@ button pressed.", title];
	statusText.text = newText;
	[newText release];
}

and that *title

Code:
	NSString *title = [sender titleForState:UIControlStateNormal];

did not have to alloc/init

while *newText

Code:
	NSString *newText = [[NSString alloc] initWithFormat: @"%@ button pressed.", title];
did?
 
Because whatever object sender is pointing to will have done the alloc/init for you as it's returning an existing object. alloc/init is used to create a new object.
 
Not sure if this will help or not, but I'll give it a whirl:
NSString *title
Is a pointer. There is no space in title for an NSString, only space for a memory address that points to an NSString. In both of your examples you're assigning an address to title. In one case you just created and own the NSString title points to, in the other the pointer returned points to an NSString that you don't own.

alloc/init do not setup space in title for an NSString, they allocate space on the heap, initialize the object, and you get back a pointer to it.

-Lee
 
OK, so let's see if I got it:

Alloc/Init are only for objects.

Since the pointer is not an object, but a variable to hold the address of an object, there is no need to alloc/init a pointer.

Multiple pointer variables can point to the same object.

The method alloc/init the object will be responsible for releasing it.

(I guess I hadn't known that I do not need to worry about allocating pointers since they are so intrinsically tied to object allocation; but we do declare the pointers just like primitives in the @interface, I presume).
 
OK, so let's see if I got it:

Alloc/Init are only for objects.
Yes. C-style allocation happens via malloc and friends, instead.

Since the pointer is not an object, but a variable to hold the address of an object, there is no need to alloc/init a pointer.
A pointer is a primitive. The memory for it is generally on the stack with other local primitives.

Multiple pointer variables can point to the same object.
Yep.

The method alloc/init the object will be responsible for releasing it.
Think of it as ownership. Look at apple's memory management guide. alloc, new, and copy methods give you a pointer to an object you own. Any other method that returns an object pointer points to an object you don't own.

(I guess I hadn't known that I do not need to worry about allocating pointers since they are so intrinsically tied to object allocation; but we do declare the pointers just like primitives in the @interface, I presume).

They are primitives, yes. They are the only way to get to Objective-C objects, but they can point to primitives, including other pointers, and functions, too. Before there were objects there were pointers in C.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.