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

namanhams

macrumors regular
Original poster
Jun 3, 2009
153
0
Hi everyone,

I'm using Iphone SDK 3. There's some funny thing i've just seen and here's the code :

A.
Code:
- (void) someMethod { 
     NSArray *dumbArray;
     NSLog(@"%@", dumbArray)
}

==> Application crashes.

B.
Code:
- (void) someMethod { 
     NSLog(@"xyz");
     NSArray *dumbArray;
     NSLog(@"%@", dumbArray)
}

==> it prints out the current object that we send the someMethod message.
Actually i've tested in some places and sometimes it crashes.

C.

- (void) someOtherMethod {
NSArray *a = [NSArray arrayWithObjects : @"1", @"2",nil];
NSArray *dumbArray;
NSLog(@"%@", dumbArray);
}

==> it prints out the content of array 'a'


For B and C, i think in any circumstances it could not do that.
For A, i'm not sure but i think it should print out 'null'



Any idea ?
 
Is dumbArray a string? No, it's an array. If you want a string description of an array use [dumbArray description].
 
As far as I know uninitialised pointers can point anywhere.

So sometimes to data which is recognisable, and sometimes to null (or whatever) which results in crashes when accessed.
 
I think NSLog can be used to print any object.
Actually, on checking you're right. NSLog automatically tries to use the description method on objects to get a string value. I've always used an explicit call to it just in case.
 
As far as I know uninitialised pointers can point anywhere.

So sometimes to data which is recognisable, and sometimes to null (or whatever) which results in crashes when accessed.

Yep i believe this is true, which is why sometimes you will get a result and sometimes you won't (because it will be pointing to something it cannot print)
 
I've noticed that objective-c can be quite temperamental, so to keep it safe, always use [objectName description], equivalent to java's objectName.toString();
 
Thanks for all the replies. Now i know why sometimes when i define something as type A but XCode keeps claiming type B, C or D.... When i want to use nil value, i have to explicitly set its value to nil first.
 
the declaration NSAnything *myObject tells the compiler that myObject will have to be considered of type NSAnything. some compilers would add the necessary low level code to reserve 4 bytes, or 8 bytes, or whatever is sufficient, to store a number that will represent the memory pointer to your object when it will be created; some wont. some compilers may go the extra mile and set that pointer value to 0; some wont. however, this behaviour is not documented, and you shouldnt assume that if something happens once, it will always happen. to be more specific, there is no guarantee that your 3rd example will always work the same. an unassigned variable in your program is just that, an unassigned variable, and it's only luck that makes it point to anything at all.
 
This sounds like it could be a common error for people who try to learn Obj-C without learning C.

Obj-C objects initialize their object pointer instance variables by default.

C does not initialize its variables unless it's done explicitly.
e.g.
NSArray *dumbArray = nil;

Accessing an uninitialized variable is usually considered an outright error by static analysis tools.

So this is a bug in the OP's knowledge, not the SDK.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.