Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 27th, 2006, 8:31 AM   #1
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3 Soulstorm is on a distinguished road
Objective-C: When do I need to Allocate?

I have post it into another forum as well, but I would like some answers here, too.

I am currently a newbie to Obj-C, and I have a strong background in C++. Here are some lines of code that I would like to have explained:
int main(int argc, char* argv[]){
	fraction *a = [[fraction alloc] init];
	fraction *b = [[fraction alloc] init];

	fraction *result;
	
	[a setTo: 1 over: 3];
	[b setTo: 2 over: 5];
	[a print];
	printf(" + ");
	[b print];
	printf(" = ");
	result = [a add: b];
	[result print];
	printf("\n");
	[result free];

	[a print];
	printf(" - ");
	[b print];
	printf(" = ");
	result = [a sub: b];
	[result print];
	printf("\n");
	[result free];
	
	[a print];
	printf(" * ");
	[b print];
	printf(" = ");
	result = [a mul: b];
	[result print];
	printf("\n");
	[result free];
	
	[a print];
	printf(" / ");
	[b print];
	printf(" = ");
	result = [a div: b];
	[result print];
	printf("\n");
	[result free];
	
	return 0;
}
You will probably understand that I have a fraction class declared elsewhere and some methods, which are of no importance to my question.

As you can see, I am pre-allocating the "fraction *a" and "fraction *b" but NOT "fraction *result". Why is that? How come it's not necessary to write
fraction *result = [[fraction alloc]init]
to allocate memory for the "result" object?

I know that if I eventually write "fraction *result = [[fraction alloc]init]" it will also be correct, but why doesn't it give me any errors?

NOTE: In the "-(void) setTo: (int) n over: (int) d" function that you see into the "main()" I do not allocate memory for my object

In general, I have a hard time trying to figure out when must I allocate objects using the "alloc" method and when not. I also have a hard time to avoid memory leaks (for example, I know that I shouldn't write "[[a div: b] print]" because an object will be created that I won't be able to free later).

If you still can't figure out what I am saying, here is the entire source.
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old Apr 27th, 2006, 10:39 AM   #2
sykkn
Hobbyist Programmer
 
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5 sykkn is on a distinguished road
-(fraction *) add: (fraction *) f
{
	fraction *result = [[fraction alloc] init];
	int resultNum, resultDenom;
	
	resultNum = (numerator * [f denominator])
				+(denominator * [f numerator]);
	resultDenom = denominator * [f denominator];
	[result setTo:resultNum over:resultDenom];
	[result reduce];
	return result;
}

there is your answer ... you do not need to allocate for result because it is allocated in a's add method ... then a pointer to the result is passed back and stored in your result var.
__________________
[ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
/* Don't make me use it! */
sykkn is offline   Reply With Quote
Old Apr 27th, 2006, 12:57 PM   #3
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3 Soulstorm is on a distinguished road
The result that I am talking about isn't initialized and allocated. The 'result' in main() itself is not allocated nor initialized in the 'add' method. Inside the method, a new object is created, and then it is initialized, but the fact that they have the same name with the 'result' in the main() doesn't mean they are the same object.

...or am I wrong?

Also, someone said I have produced a memory leak here... Could you explain me why? the other thread is here
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old Apr 27th, 2006, 2:44 PM   #4
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 4 Kaja Fumei is on a distinguished road
Yes, the result you are talking about is initialized and allocated several times, just not in main. When main runs this line:
result = [a add: b];
it calls the add method found in the "fraction.m" file:
-(fraction *) add: (fraction *) f
{
	fraction *result = [[fraction alloc] init];  // point of interest #1
	
        // skipping over the rest of the computation because its not necessary
        // to the explanation

	return result;    // point of interest #2
}
The very first line creates a local variable called "result" which is a pointer to a fraction (the fact that it has the same name as the variable in main is irrelevant), and allocates a new fraction and stores the pointer to the new fraction in that local variable. The add method does its computations, and at the end, returns the pointer stored in the local "result". Then, according to the first line, I posted, that pointer is stored in main's "result" variable.
The after main's result is printed, main calls:
[result free];
which frees the allocated memory stored pointed to by result. Then as you perform the subtraction, multiplication, and division, the same cycle goes on: the methods return the result with new allocated memory which is freed after its printed.

On a side note, your code never frees "a" and "b" so add:
[a free];
[b free];
above the return 0 in main.
Kaja Fumei is offline   Reply With Quote
Old Apr 28th, 2006, 4:57 AM   #5
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3 Soulstorm is on a distinguished road
Thanks a lot, that was quite helpful.

Quote:
On a side note, your code never frees "a" and "b" so add:
[a free];
[b free];
above the return 0 in main.
Heh. I figured that out later. Thanks a lot.
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:42 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC