Amazon.com Widgets attempted answer to exercise 17-2
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 22, 2013, 11:57:25 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 17
| | | |-+  attempted answer to exercise 17-2
Pages: [1]   Go Down
Print
Author Topic: attempted answer to exercise 17-2  (Read 897 times)
mdeh
Full Member
***
Posts: 166






« on: February 23, 2009, 09:03:13 PM »

Code: (Objective-C)
#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSString.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSData.h>

#define ARRAY_CAPACITY 10

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray * myArr = [NSMutableArray arrayWithCapacity: ARRAY_CAPACITY];
NSNumber *first_number, *replacement_number;

first_number = [NSNumber numberWithInt: 42];
replacement_number = [NSNumber numberWithInt: 17];

NSLog(@"Retain Count: \"First Number:\" = %i", [first_number retainCount]);
NSLog(@"Retain Count: \"Replacement Number:\" = %i", [replacement_number retainCount]);

[myArr addObject: first_number];


NSLog(@"Add \"First Number\" to array");
NSLog(@"Retain Count: \"First Number:\" = %i", [first_number retainCount]);
NSLog(@"Retain Count: \"Replacement Number:\" = %i", [replacement_number retainCount]);

NSLog(@"Now replace \"First Number\" with \"Replacement Number\"");
[myArr replaceObjectAtIndex:myArr.count - 1 withObject:replacement_number];
NSLog(@"Retain Count: \"First Number:\" = %i", [first_number retainCount]);
NSLog(@"Retain Count: \"Replacement Number:\" = %i", [replacement_number retainCount]);

NSLog(@"\"Like NSArray, instances of NSMutableArray maintain strong references to their contents. If you do not use garbage collection, when you add an object to an array, the object receives a retain message. When an object is removed from a mutable array, it receives a release message. If there are no further references to the object, this means that the object is deallocated.\"   My comment: I imagine that \"replace\" is nothing more than \"remove\" and \"addObject\" and thus the result is to be expected.");

    [pool drain];
    return 0;
}


Note: if you use numbers from -1 through 12 you will get "strange"  retainCount numbers.
From Steve.


Code: (Objective-C)
int main (int argc, const char * argv[]) {
        NSAutoreleasePool   * pool = [[NSAutoreleasePool alloc] init];;
        NSNumber        *first_number;
        NSNumber        *n1, *n2, *n3, *n4, *n5;

        first_number = [NSNumber numberWithInt: 42];
        n1 = [NSNumber numberWithInt: 7];
        n2 = [NSNumber numberWithInt: 7];
        n3 = [NSNumber numberWithInt: 10];
        n4 = [NSNumber numberWithInt: 42];

        NSLog (@"%i %i %i %i %i", [first_number retainCount], [n1 retainCount],
        [n2 retainCount], [n3 retainCount], [n4 retainCount]);
        [pool drain];
}

Output: 1 3 3 2 1



Quote


So the retain count for the number 42, even though repeated, is still 1.  The repeat count for 10, which appears once, is 10, and the retain count for 7, which appears twice is 3.

I found a discussion on the Internet that the numbers -1 to 12 are used so often that they're "pre-generated" and then is the number is used in the program, the compiler will use the pre-generated number and bump its retain count by 1.  So, for a number from -1 to 12, its retain count will be n + 1 if it's used n times in the program (7 is used twice, so its retain count is 2 + 1 = 3).  The first retain count of 1 comes from the original pre-generated object   Note that this happens at compile time: the compiler sees the reference to the pre-generated (or "cached") number object, creates a reference to that object, and appropriately sets its retain count to the total number of times it's used plus 1.  Also note that 42 is used twice, but since it's not in the range of -1 to 12 a new number object gets created each time and thus each object has a retain count of 1.


BTW....here is the ref to the above.http://developer.apple.com/releasenotes/Cocoa/FoundationOlder.html (thanks to Eddietr of macrumors)


« Last Edit: February 24, 2009, 06:10:50 AM by mdeh » Logged
SenX
Newbie
*
Posts: 9






« Reply #1 on: February 23, 2009, 10:09:11 PM »

Hmm, ok the exercise outcome is pretty straight forward indeed and as expected.

Interesting here though is the follow up discussion Smiley.

though i am surprised that 42 is not also part of the precached numbers here  Grin.
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

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 (c) 2009 classroomM.com. All rights reserved.