Jump to content

Recommended Posts

I've bolded the part of the code I don't fully understand.

 

  • Won't sum = sum2 just overwrite the old sum? So why do you need to release it beforehand?
  • Why isn't sum2 released at the end of the program? Should it be?

 

#import “Fraction.h”

int main (int argc, char *argv[])
{

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *aFraction = [[Fraction alloc] init]; Fraction *sum = [[Fraction alloc] init], *sum2; int i, n, pow2;

[sum setTo: 0 over: 1]; // set 1st fraction to 0

NSLog (@”Enter your value for n:”);
scanf (“%i”, &n);
pow2 = 2;

for (i = 1; i <= n; ++i) {
     [aFraction setTo: 1 over: pow2];
     sum2 = [sum add: aFraction];
[b]    [sum release]; // release previous sum
     sum = sum2;[/b]
     pow2 *= 2;
}

NSLog (@”After %i iterations, the sum is %g”, n, [sum convertToNum]); [aFraction release];
[sum release];
[pool drain];

return 0;
}

 

Thank you!!

Link to comment
https://forums.phpfreaks.com/topic/208121-understanding-release/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.