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

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,453
4,153
Isla Nublar
EDIT: Oops! This thread should read: "Why doesn't this object need memory allocated?" :D


Hi guys!

Not sure where my other thread went but I can't find it so I'm reposting the same question:

I'm coming from C++ and learning Objective-C and was working through some exercises and this one has me stumped on why its working.

Code:
#import <Foundation/Foundation.h>
#import "Complex.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Complex *c1 = [[Complex alloc] init];
	Complex *c2 = [[Complex alloc] init];
	Complex *compResult;  //<---Why doesnt ths need initialized like the above two objects?
	
	[c1 setReal: 18.0 andImaginary: 2.5];
	[c2 setReal: -5.0 andImaginary: 3.2];
	
	//Add and print two complex numbers
	
	[c1 print]; NSLog(@"     +"); [c2 print];
	NSLog(@"------");
	compResult = [c1 add: c2];
	[compResult print];
	NSLog(@"\n");
	
	[c1 release];
	[c2 release];
	[compResult release];
	
	
	
    [pool drain];
    return 0;
}

How come the last object does not need initialized the same as the others? I can initialize it and it works but if I do not initialize one of the first two objects (c1 and c2) the program wont work. I don't understand how this works seeing as they are all objects of the same type.

Could anyone explain this to me please?
 

ChOas

macrumors regular
Nov 24, 2006
139
0
The Netherlands
EDIT: Oops! This thread should read: "Why
How come the last object does not need initialized the same as the others? I can initialize it and it works but if I do not initialize one of the first two objects (c1 and c2) the program wont work. I don't understand how this works seeing as they are all objects of the same type.

Could anyone explain this to me please?

compResult is just a pointer. The reason you do not alloc memory to it is that you just point it to the place where the memory for it was allocated:

Code:
compResult = [c1 add: c2];

add: allocs and inits the memory and then you point compResult to that space.

Thats also why you release compResult later, it releases the memory allocated by add:.

If you alloc and init it like you describe you will have a leak:

Code:
compResult points to compResultInitedMemory.
[stuff]
compResult = [c1 add: c2];
compResult now points to the memory alloced by add:

... And now nobody knows where the original compResultInitedMemory is because nothing is pointing to it anymore. So it can't be released anymore.
 

larkost

macrumors 6502a
Oct 13, 2007
534
1
The other thing that you are missing is that when the [Complex add:] method returns the new Complex object, it has auto-allocated the memory for you, and dropped it into the default NSAutoreleasePool for you. So when that pool gets drained the new Complex objects gets released, and since it only has a count of 1, it gets wiped away and the memory for it released.
 

mdeh

macrumors 6502
Jan 3, 2009
345
2
The other thing that you are missing is that when the [Complex add:] method returns the new Complex object, it has auto-allocated the memory for you, and dropped it into the default NSAutoreleasePool for you. So when that pool gets drained the new Complex objects gets released, and since it only has a count of 1, it gets wiped away and the memory for it released.

Hi larkost,
I think this is one of the examples ( or could be ) from Steve Kochan's book. IIRC, the reason that "compResult" is released, is that the return value is *not* autoreleased, as I do not think this has been covered. Which may have been the source of confusion to the OP.
But, I could be totally incorrect.
 

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,453
4,153
Isla Nublar
Yep its from Steve Kochans book.

This book is a little confusing because it does use things without explaining them first, only to explain them later. Coming from C++ I can figure out pretty much anything but there are some differences that end up catching up with me. Memory management is definitely one of them.
 

mdeh

macrumors 6502
Jan 3, 2009
345
2
Yep its from Steve Kochans book.

This book is a little confusing because it does use things without explaining them first, only to explain them later. Coming from C++ I can figure out pretty much anything but there are some differences that end up catching up with me. Memory management is definitely one of them.

Hi chrono,
Steve Kochan actually has a web site that supports the book. I think the **single** commonest question is about this issue :)
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
This is what people mean when they say convenience methods.
The method returns a new object, HOPEFULLY autoreleased so you can retain or copy it if you want to keep it. And it will go away on its own in the future if you do not.

One of my favorites is...
int i=25;
NSString *newString = [NSString stringWithFormat: @"my i = %d", i ];

Though in this context you'd have to retain it if you wanted to keep it.

Usually its particularly useful if you have instance variables that are using @property (retain)
 

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,453
4,153
Isla Nublar
Hi chrono,
Steve Kochan actually has a web site that supports the book. I think the **single** commonest question is about this issue :)

I saw it but my work proxy thinks its "Provocative Attire" and I live where I work so I can't access it : /

Thank you for the suggestion though :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.