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

Branda22

macrumors newbie
Original poster
Jun 17, 2013
25
0
Chicago, IL
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.
 

Branda22

macrumors newbie
Original poster
Jun 17, 2013
25
0
Chicago, IL
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.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,563
6,062
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.

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.
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
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.

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
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,563
6,062
It does not allocate anything.

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.
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
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.

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.
 

Branda22

macrumors newbie
Original poster
Jun 17, 2013
25
0
Chicago, IL
What about when I use NSString?

Code:
 NSString *myString = @"Hello!";

There is no alloc init and we have @"Hello" stored in that pointer.
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
What about when I use NSString?

Code:
 NSString *myString = @"Hello!";

There is no alloc init and we have @"Hello" stored in that pointer.

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];


----------

What about when I use NSString?

Code:
 NSString *myString = @"Hello!";

There is no alloc init and we have @"Hello" stored in that pointer.

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.
 

Branda22

macrumors newbie
Original poster
Jun 17, 2013
25
0
Chicago, IL
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.
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
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.


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.
 

firewood

macrumors G3
Jul 29, 2003
8,108
1,345
Silicon Valley
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.

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".
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
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".

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.
 

firewood

macrumors G3
Jul 29, 2003
8,108
1,345
Silicon Valley
Using agreed-upon terminology is a good thing.

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.
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
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.

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.
 

firewood

macrumors G3
Jul 29, 2003
8,108
1,345
Silicon Valley
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.
 

firewood

macrumors G3
Jul 29, 2003
8,108
1,345
Silicon Valley
What about when I use NSString?

Code:
 NSString *myString = @"Hello!";

There is no alloc init and we have @"Hello" stored in that pointer.

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.
 

Branda22

macrumors newbie
Original poster
Jun 17, 2013
25
0
Chicago, IL
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.

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]
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.