When to use [[alloc]init]

Discussion in 'iOS Programming' started by Branda22, Jun 17, 2013.

  1. Branda22 macrumors newbie

    Joined:
    Jun 17, 2013
    Location:
    Chicago, IL
    #1
    I'm confused as to when I need to use alloc and init with foundation objects. I thought when I instantiated an NSMutableArray object for example
    Code:
     NSMutableArray *myNSMutableArray; 
    the alloc and init was done behind the scenes, but i've read some code where the alloc and init was done manually.

    Thanks.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    What resources are you using to learn iOS / Objective-C Programming? Please be as specific as possible.
     
  3. Branda22 thread starter macrumors newbie

    Joined:
    Jun 17, 2013
    Location:
    Chicago, IL
    #3
    I've been watching the iTunes U course, and lynda.com videos.

    I understand I have to alloc and init my own classes, but I get confused about when to alloc and init built in classes.
     
  4. ArtOfWarfare macrumors G3

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    Always?

    You're not creating anything other than a pointer that should point at an NSMutableArray in your code above - but in your code since you've assigned it to nothing at all, it either points at address 0 which contains nil or garbage, depending on the implementation (I don't think the standard specifies what an uninitialized pointer should point at.)

    If you assign it to an [[alloc] init] or [new] or [arrayWith...:] or @[] then it'll point at something after that.
     
  5. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #5
    This is a fundamental thing about C-like languages.


    There are declarations, allocations, and assignments.

    The code you posted declares a variable of type NSMutableArray. It defines a pointer variable that CAN point to an object.

    It does not allocate anything.

    alloc/init does allocate an object.

    In order to create an object and store it somewhere, you need to

    declare a variable
    allocate an object
    assign the newly allocated object to the variable.

    What you posted was a declaration.
    Code:
    [[Classname alloc] init]
    allocates an object.
    The assignment
    Code:
    myNSMutableArray = [[NSMutableArray alloc] init];
    
    allocates a new object and assigns it to the variable myNSMutableArray
     
  6. ArtOfWarfare macrumors G3

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #6
    It allocates a pointer. It doesn't allocate anything for that pointer to point to. That pointer is on the stack (assuming it's in the body of a function or method) while the thing being pointed to would be on the heap.
     
  7. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #7
    Don't use the word allocate. It doesn't allocate anything. It defines a pointer variable. In computer science allocation means something very specific, and that ain't it.
     
  8. Branda22 thread starter macrumors newbie

    Joined:
    Jun 17, 2013
    Location:
    Chicago, IL
    #8
    What about when I use NSString?

    Code:
     NSString *myString = @"Hello!";
    There is no alloc init and we have @"Hello" stored in that pointer.
     
  9. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #9
    The "@" sign tells the compiler to create a string literal. Under the covers, it calls code that allocates an immutable string object.

    Until recently, strings were the only things you could create using literal syntax. However, Apple recently added support for other types of object literals:

    Code:
    @10
    Creates an NSNumber with the value 10:

    Code:
    @[@"string1", @"string2", @"string3"];
    
    Creates an NSArray containing 3 strings.

    Under the covers, the compiler generates code equivalent to

    Code:
    [NSArray arrayWithObjects: @"string1", @"string2", @"string3", nil];


    ----------

    The key point is that

    NSString *someString;

    or

    SomeType *someVar;

    NEVER creates a string object. It declares a pointer that can be used to point to a string object.

    That goes for any declaration. It never creates an object - it just defines a pointer variable that CAN point to an object, or to nothing at all. A zero (or nil) pointer is a pointer that doesn't point to anything.

    You need some code to go on the right side of an "=" assignment statement to assign a value to a pointer variable..

    There are a variety of different things you can put on the right side of an assignment that will allocate an object for you.

    @"string" is a form that creates a static string object.

    The NSString class method stringWithFormat will create a string object from a format string and zero or more other parameters.
     
  10. Branda22 thread starter macrumors newbie

    Joined:
    Jun 17, 2013
    Location:
    Chicago, IL
  11. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #11
    Does that clarify things for you?
     
  12. romiebleed macrumors member

    Joined:
    Aug 22, 2011
    Location:
    johannesburg
    #12
    Thank you very much

    This definitely clarify lot of things for me :)

    Thanks again.
     
  13. Branda22 thread starter macrumors newbie

    Joined:
    Jun 17, 2013
    Location:
    Chicago, IL
    #13
    From what I understood.

    When I write
    Code:
    NSString *myString = @"Hello";
    myString is a pointer to an address in memory where I can store an NSString object. This is done because the compiler does not know the size of the object, unlike a char or an int which always occupy the same space in memory.
    Code:
    @"Hello"
    the @ sign tells the compiler todo some behind the scenes code to alloc and init among other things.
     
  14. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #14

    Sort of. Objects are pointers because that is how the memory for objects is managed in Objective C. Objects are allocated dynamically from the heap.

    Local scalar variables are allocated on the stack.

    If you don't understand the terms heap and stack you don't have a strong enough foundation in computer memory to get it yet.
     
  15. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #15
    And sometimes computer science terminology spits out garbage nonsense. Depending on the implementation/optimization, there may or may not be some number of bits in addressable physical or virtual memory that are allocated to holding a (initialized or garbage) pointer value, in spite of misnamed "scientists" making up definitions saying it ain't "allocated".
     
  16. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #16
    Using agreed-upon terminology is a good thing.

    You're free to use the wrong terms for things if you want to, but then

    a) You'll be wrong

    b) you confuse those that don't know any better,

    and

    c) those that do know what they are talking about are likely to ignore you.

    Local variables are created on the stack, which is already allocated from the virtual memory system in advance.

    Instance variables are stored in the data block that is ALLOCATED to the object. All the instance variables are part of that storage area, which is ALLOCATED all at once when the object is created.
     
  17. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #17
    Only if the agreement is made in advance of the conversation. Not forced upon some random to prove that one is an uppity "real scientist".

    Assuming that the stack and heap are actually real things in a typical flat addressable memory process is a good way to not understand why their buggy code crashes. It's all just bits. Some useful, not not.
     
  18. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #18
    I should have said "Widely accepted industry standard terminology".

    Engineering disciplines tend to use very specific jargon, as do most technical fields.

    That jargon allows those familiar with it to communicate specific concepts clearly.
     
  19. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #19
    Jargon is useful between people who understand the jargon. Someone who has no clue about some memory garbage that they think is an object reference needs something more like baby talk. Not jargon.
     
  20. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #20
    Yes there is. While you weren't looking, the compiler genie was cooperating with the loader demon and runtime fairy to alloc and init some bits for your = to assign. Magic while you app was launching.

    If you don't believe that, then there are a half dozen semesters worth of SE/EE/CS courses that might expose these magic tricks for what they really are.
     
  21. Branda22 thread starter macrumors newbie

    Joined:
    Jun 17, 2013
    Location:
    Chicago, IL
    #21
    I understand that the stack deals with things such as local variables, in that it gets thrown out once not in use.

    things like objects, and global variables are dealt by the heap, they remain in existence during the duration of the program.


    In regards to when to alloc and init I now understand that
    literals are dealt with the "@" sign but some factory methods have convenience constructors like [NSDate date] where the return value is an allocation and initialization.

    Code:
     return [[[self alloc]init] autorelease]   
     

Share This Page