I know Kochan will be covering this in much greater detail later, but I want to clarify one thing now
Firstly, here is the code:
So, here is the question.
There are 2 circumstances in the above code where a new value is assigned to a Fraction object, A and B as marked.
In "A" it seems that the new values are being assigned to the same object, so no "memory leak"?
But in B, sum2 is a different object from which "sum" is currently assigned to. Here is my question. If the above scenerio is correct, **if sum were **not** freed** why would the memory of "sum" simply not be handed back to the system? Does the system not intuitively know when memory is no longer being "used". If the words are rather inarticulate, it's probably because I am missing something quite basic here, but would appreciate some input.
Thanks in advance.
Firstly, here is the code:
Code:
Fraction *aFraction = [ [Fraction alloc] init];
Fraction *sum = [ [ Fraction alloc] init], *sum2;
int i, n, pow2;
[sum setTo: 0 over: 1];
NSLog(@"Enter your value for n");
scanf("%i", &n);
pow2 = 2;
for ( i = 1; i <= n; i++){
[aFraction setTo: 1 over: pow2]; /* A */
sum2 =[aFraction add: sum];
[sum release];
sum = sum2; /* B */
pow2 *= 2;
}
So, here is the question.
There are 2 circumstances in the above code where a new value is assigned to a Fraction object, A and B as marked.
In "A" it seems that the new values are being assigned to the same object, so no "memory leak"?
But in B, sum2 is a different object from which "sum" is currently assigned to. Here is my question. If the above scenerio is correct, **if sum were **not** freed** why would the memory of "sum" simply not be handed back to the system? Does the system not intuitively know when memory is no longer being "used". If the words are rather inarticulate, it's probably because I am missing something quite basic here, but would appreciate some input.
Thanks in advance.