Confused on ivar allocation

Discussion in 'Mac Programming' started by zippyfly, Sep 17, 2010.

  1. macrumors regular

    Joined:
    Mar 22, 2008
    #1
    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?
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    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
     
  4. thread starter macrumors regular

    Joined:
    Mar 22, 2008
    #4
    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).
     
  5. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    Yes. C-style allocation happens via malloc and friends, instead.

    A pointer is a primitive. The memory for it is generally on the stack with other local primitives.

    Yep.

    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.

    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
     
  6. thread starter macrumors regular

    Joined:
    Mar 22, 2008
    #6
    Many thanks! This really helped me clear up some stuff.
     

Share This Page