Memory Leak - Can you better explain this example for me?

Discussion in 'iOS Programming' started by ruffy, Nov 5, 2010.

  1. ruffy macrumors member

    Joined:
    Jan 10, 2008
    #1
    An arithmetic progression 1/2, 1/4, 1/8, etc. needs summation after n loops.
    The relevant code from the main procedure:
    Code:
            Fraction *aFraction = [[Fraction alloc] init];
    	Fraction *sum = [[Fraction alloc] init], *sum2;
    	int i, n;
    	int pow2 = 2;
    	[sum setTo: 0 over: 1];
    	NSLog(@"Enter your value for n.");
    	scanf("%i", &n);
    	for (i=0; i<n; i++) {
    		[aFraction setTo: 1 over: pow2];	
    		[COLOR="Red"]sum2 = [sum add: aFraction];	
    		sum = sum2;
    [/COLOR]		pow2 *= 2;		
    	}
    The relevant code from the add: method follows:
    Code:
    -(Fraction *) add: (Fraction *) f    
    {
    	Fraction * result = [[Fraction alloc] init];
    	result.numerator = (numerator * f.denominator + denominator * f.numerator);
    	result.denominator = denominator * f.denominator;
    	return result;
    }
    Regarding the lines in red in main's FOR LOOP, Kochan writes:
    "... is then added to the cumulative sum by using by using the add: method. The result
    from add: is assigned to sum2 and not to sum, to avoid memory leakage... The old sum
    is then released, and the new sum, sum2, is assigned to sum for the next iteration...."

    This newbie to OOP needs a better understanding of this problem. Could someone
    please clarify this problem for me? I don't quite get this memory-tag concept.
    E.g., why are pointers used for objects, yet not for defined integers?
    (For an old-time procedural coder, this looks like a lot of heavy extra baggage.)
    I thank you.
     
  2. chown33, Nov 5, 2010
    Last edited: Nov 5, 2010

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    The problem isn't OOP vs. procedural. The problem is not knowing what a pointer is. And possibly not knowing how memory is ordered, and what an address is.

    Plain C is procedural and it has pointers. C's pointers are the same as pointers in Objective-C. They each refer to a memory location where the thing of interest currently resides. So if you understand C pointers you will understand Objective-C pointers. Conversely, if you don't understand C pointers, you will not understand Objective-C pointers.

    Some explanations of pointers:
    http://masters-of-the-void.com/book5.htm
    http://en.wikipedia.org/wiki/Pointer_(computing)
    http://boredzo.org/pointers/
    http://pweb.netcom.com/~tjensen/ptr/cpoint.htm


    I think you're missing something, because I see no releases in the posted code.
     
  3. ruffy thread starter macrumors member

    Joined:
    Jan 10, 2008
    #3
    Thanks chown33.

    I'll do the homework you gave me.

    By the way, I omitted the "releases" coded, just to show
    what was relevant for my question.

    But in passing, I'd like to ask you:
    Could you not add that 1/2n fraction to a static number,
    and have that number increment with every loop, and pass
    on that value to main?

    What I'm really asking is - is there some advantage I
    don't see in the above example in its method of
    accumulation
    ?

    Thanks again chown33.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    That's a bad idea. You're a beginner. What you think is irrelevant may be very important. It's always better to post complete code, especially for things that aren't that big.

    You can do whatever you want, if you're designing and writing the class.

    A generalized add method should not modify either of its operands, so it can be used for general arithmetic. If you want a specialized method that adds to an operand, you can make one.
     
  5. ruffy thread starter macrumors member

    Joined:
    Jan 10, 2008

Share This Page