PDA

View Full Version : Confused on ivar allocation




zippyfly
Sep 17, 2010, 04:02 PM
Hello. Why is it:

@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


NSString *title = [sender titleForState:UIControlStateNormal];


did not have to alloc/init

while *newText

NSString *newText = [[NSString alloc] initWithFormat: @"%@ button pressed.", title];

did?



robbieduncan
Sep 17, 2010, 04:04 PM
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.

lee1210
Sep 17, 2010, 04:25 PM
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

zippyfly
Sep 17, 2010, 09:16 PM
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).

lee1210
Sep 17, 2010, 09:56 PM
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

zippyfly
Sep 17, 2010, 11:06 PM
Many thanks! This really helped me clear up some stuff.