Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ruffy

macrumors member
Original poster
Jan 10, 2008
42
0
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.
 
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


The old sum is then released,
I think you're missing something, because I see no releases in the posted code.
 
Last edited:
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.
 
By the way, I omitted the "releases" coded, just to show
what was relevant for my question.
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.

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?
You can do whatever you want, if you're designing and writing the class.

What I'm really asking is - is there some advantage I
don't see in the above example in its method of
accumulation
?
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.