Question

Maybe my memory has gone completely wacko, but I think I remember that declaring pointers without initializing them made them point to nil. But recently this doesn't seem to be the case. Has it always been this way? Or has it something to do with the compiler settings?

Was it helpful?

Solution

Instance variables of objects are initialized to nil in alloc (the whole object is bzeroed).

Edit: Also, global and static storage variables are initialized to zero (6.7.8 10 of the C99 Standard, thanks Derek for pointing that out).

Local stack variables are not initialized automatically. This did not change in known history.

OTHER TIPS

From Transitioning to ARC Release Notes:

Stack Variables Are Initialized with nil

Using ARC, strong, weak, and autoreleasing stack variables are now implicitly initialized with nil. For example:

- (void)myMethod {
    NSString *name;
    NSLog(@"name: %@", name);
}

will log null for the value of name rather than perhaps crashing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top