Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 16, 2017, 09:10:04 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Prog 7.5 - query about definition of result object  (Read 2230 times)
GreatPumpkin
Newbie
*
Posts: 12


« on: September 12, 2011, 08:45:45 AM »

Hi,

I'm unsure why the result object is defined in main.m on page 142 / location 3340 with the following:

    Fraction * resultFraction;

without allocating memory (unlike fractions a and b defined immediately above) and is then defined in full from within the add method in the implementation section:

    Fraction * resultFraction [[Fraction alloc] init];

Why can I not just put the full definition in main.m and no definition in the implemenatation section like the other fractions?  Why does it need to be partially defined twice?

Thanks in advance for any clarification.
Logged
seerex
Full Member
***
Posts: 177


Email
« Reply #1 on: September 12, 2011, 08:52:27 AM »

When you add your 2 Fractions, you are creating a new Fraction object, so you won't make changes to the original ones passed in. For example, you could have your add method modify one of the passed in values (a or b) but then you would change that value permanently. It makes more sense to have the method allocate and initialize a new Fraction object and then return it. Now, you must assign the result of this addition to a non-allocated / non-initialized variable that can hold a Fraction object, coz if you don't, you won't have any way of releasing it after it is created "behind the scenes" in your method.

Hope i helped, if not, let me know and i'll try harder Smiley

Best regards
/JBJ
http://www.jbjprogramming.com/public_html/Home.html
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
GreatPumpkin
Newbie
*
Posts: 12


« Reply #2 on: September 12, 2011, 09:01:09 AM »

Thanks.

I understand the point of creating a new object to store the result in, but not the way the object is created.

If the object had memory defined and allocated in main.m why could I not still release that memory at the end of main.m?

I don't get why memory has to be allocated within the method itself, especially since the object has already been partially defined within main.m and is then released within main.m.

Many thanks indeed, I greatly appreciate your help.
Logged
GreatPumpkin
Newbie
*
Posts: 12


« Reply #3 on: September 12, 2011, 09:13:17 AM »

Just to illustrate my confusion, I realise I made a mistake in my post above, but think I might almost be starting to understand part of this.  Part of it still mystifies me though.

The add method doesn't define and allocate resultFraction as I thought, just an object called result. Apologies for adding to my own confusion with this mistake.

So main.m defines (but doesn't allocate) resultFraction.

resultFraction is set to equal the result of the add method.  The add method defines and allocates memory to an object called result. This is then returned to resultFraction.

So why does main.m release memory for resultFraction which hasn't had any memory allocated to it, but does not release result which has had memory allocated?

The text states:

"Note that you must be careful at the end of the program to release resultFraction, even though you didnít allocate it yourself in main. The add: method allocated it, but itís still your responsibility to clean it up."

But the add method didn't allocate memory to resultFraction, it allocated it to result instead.

What's the difference between the objects "result" and "resultFraction"?

Thanks again.
« Last Edit: September 12, 2011, 09:30:44 AM by GreatPumpkin » Logged
seerex
Full Member
***
Posts: 177


Email
« Reply #4 on: September 12, 2011, 12:05:45 PM »

Ah, now i think i follow you Smiley

When you create an object, like this:
Code: (Objective-C)
Fraction *resultFraction = [[Fraction alloc] init];
You are making a variable called "resultFraction". This variable is set to hold a REFERENCE, or pointer to an object. That means, that resultFraction actually doesn't hold any value itself.

When you call the alloc and init method, you are creating an object, and that object is placed in memory. Those methods, will then return the address to the resultFraction. That means, resultFraction holds the address in memory to the object. Hence the reason why resultFraction actually just ---> points / reference the object that was created by Fraction alloc / init.

Maybe this video will help - it's about another exercise in the book, but i still show a little how memory is handled. I also have a video tutorial on my site below that might help, about allocating and initializing objects, and as stated on my site i get a lot of my inspiration from the book so the terminology might seem similar - http://www.youtube.com/watch?v=HXQ7nco_M8w

When you then have created that object in your add method, you store the memory address in the variable created inside your add method. When you have your add method return that variable, you actually just return the memory address / the reference / pointer. Then you store that memory address / reference / pointer in the other variable which you just made in your main routine. Now, they both point to the same object in memory. When you then call the release method on the variable created in m, it destroys the object, even though it was created in your add method, both those variable pointed to the same object. Now that the object is destroyed on one of the variables, you successfully destroyed the whole object and cleaned memory out.

Although, don't be too scared if you don't understand. The new ARC feature is about to make all this happen automatically (cleaning up and releasing after you) but i still think it's a good idea to know a little about it since a lot of code is written this way since the ARC is very new and not commonly used yet.

Hope i helped Smiley

Best regards
/JBJ
http://www.jbjprogramming.com/public_html/Home.html
« Last Edit: September 12, 2011, 12:08:04 PM by seerex » Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
GreatPumpkin
Newbie
*
Posts: 12


« Reply #5 on: September 13, 2011, 03:43:20 AM »

Brilliant, many thanks, I *think* I fully understand this now.

Much appreciated.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ÔĹ© 2009 classroomM.com. All rights reserved.