Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
October 16, 2017, 08:48:09 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: Chapter 7: allocation within a method  (Read 2318 times)
pif
Newbie
*
Posts: 28


« on: July 27, 2015, 04:23:07 AM »

Quote
Inside the method, a new Fraction object is allocated and the resulting addition is performed. The result that is stored in the Fraction object result is then returned by the method, where it is then stored in the variable resultFraction.

I don't really understand how does this work. My understanding was that all the variables initialized inside methods work only locally and by extension - any local allocation and initializing would also be wiped out after exiting the method.

Logged
jonr
Full Member
***
Posts: 155


« Reply #1 on: September 10, 2016, 04:23:33 PM »

This is a good question and one that I never really thought about.  Well, at least in the context of object allocation within a method.  You are correct about how local variables work with regards to their scope.

I think why this is a special case is because in this example the variable (object) is a pointer.  So, it's creating a reference to a memory location, not merely assigning a value to a variable.  When you create a 'regular' variable in a method or function and initialize it, you are just assigning a value to the variable and not working at the memory address level.

I may not be doing the best job explaining this; probably someone more experienced could do a better job. 

-jonR
Logged
nunof32
Newbie
*
Posts: 2


« Reply #2 on: February 24, 2017, 06:50:44 AM »

Hello,

I just posted something very similar to this (Feb. 24, 2017) before I noticed your post.

I think your concern is valid. Have you had a full explanation on how and why this works (or not).

Regards,

nunof32
« Last Edit: February 24, 2017, 07:22:07 AM by nunof32 » 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.