![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3
![]() |
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;
}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] 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) |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5
![]() |
-(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! */ |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3
![]() |
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) |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 134
Rep Power: 4
![]() |
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]; -(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 after main's result is printed, main calls: [result free]; On a side note, your code never frees "a" and "b" so add: [a free]; [b free]; |
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3
![]() |
Thanks a lot, that was quite helpful.
Quote:
__________________
Project::Soulstorm (personal homepage) |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|