Why method scoped variables not alloc+init?

Discussion in 'Mac Programming' started by zippyfly, Jul 6, 2010.

  1. zippyfly macrumors regular

    Joined:
    Mar 22, 2008
    #1
    When allocating and initializing a new object (location), we usually do:

    Code:
    NSString *myString = [[NSString alloc] init];
    So obviously this calls the class to allocate memory, return the pointer, and initialize the memory at the pointer, and return the pointer again.

    How come I see other code that does not do the allocation?

    It would just go

    Code:
    NSString *newString;
    and then go and do the fancy stuff with that.

    Usually it is a variable within a method scope, and takes in the passed parameters from that method to work on something, before being returned as the method goes out of scope.

    Can anyone please help explain?

    Thanks!
     
  2. NickFalk macrumors 6502

    NickFalk

    Joined:
    Jun 9, 2004
  3. zippyfly thread starter macrumors regular

    Joined:
    Mar 22, 2008
  4. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    You are confused between what an object is and what a pointer is. A pointer is not an object, it refers to (points to) and object. [[SomeClass] alloc] init] creates an initialised object and returns a pointer to that object, which you need to store in some pointer variable, or you could never access that object. But often I don't want to create a new object, I want to refer to an existing object.
     
  5. zippyfly thread starter macrumors regular

    Joined:
    Mar 22, 2008
    #5
    Ok, thanks, I think I get you now.

    So you mean just like primitives, for pointer variables (anything declared with a * preceding and as a pointer to that object type), I do not need to alloc/init them and as long as I declare them then I can use them to hold a pointer (assigned from another pointer variable).

    ?
     
  6. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #6
    I think you might still be a bit confused. You need to alloc and init objects (and only objects). That process returns a pointer to the memory of that object which you need to store in a pointer variable (otherwise you've just reserved memory that you have no way of releasing later since you haven't grabbed a reference to it-- AKA a memory leak).

    There are factory methods which will create a new object for you so you don't need to call alloc init (often these are autoreleased so check the documentation on any particular factory method for the specifics).

    Pointers themselves are just integers. Think of them like the address on a mailbox. They're the number you go to to get to the container, but the mailbox itself (the object in this analogy) is what actually holds the data.

    Are you working through a book?
     
  7. whooleytoo macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #7
    When you do this:

    Code:
    NSString* myString = [[NSString alloc] init];
    
    You are actually doing several things:

    1) Creating a new pointer (myString).
    2) Using NSString to allocate a new NSString object.
    3) Calling init to initialise that new NSString object.
    4) Pointing myString at the new object.

    When you do this:
    Code:
    NSString* myString;
    
    You are only doing the first step above, creating the pointer. You haven't actually created a string, just a pointer which can point to a string.

    So for example:

    Code:
    NSString* myString;
    myString = [myRandomObject path];
    
    Here:
    1) You create the myString pointer.
    2) The 'path' method is creating a new NSString object via alloc & init (or calling another method which does so)
    3) The myString pointer is pointed at the NSString that the 'path' method returns.

    You might be confused in thinking "How can I use myString here, when I've never allocated it?". The answer is: you don't have to in the example above. If you call a method which returns an object, that method will do the "allocing" and "initing" for you.

    Hope that helps. :)
     
  8. misee macrumors member

    Joined:
    Jul 4, 2010
    #8
  9. zippyfly thread starter macrumors regular

    Joined:
    Mar 22, 2008
    #9
    Hi everyone,

    Ok thanks, I understand and you've confirmed my understanding.

    I guess the hurdle was understanding that pointers can be declared separately and used separately, since pretty much all examples do everything together.

    I understand now that the object(s) that the declared pointer points to are from elsewhere and initialized elsewhere, and the declared pointer variable is just taking the memory address from the (original) pointer to that object.

    Thanks for your help! Big fundamental concept cleared up for me!
     

Share This Page