Reputation: 7178
I.e., does ObjectiveC behave like C, in that if you create a pointer eg:
NSString* c;
Does c point to nil, or to the random value in the memory area that was reserved for it?
In other words, do pointers get pre-initialized, or do I need to do that myself? As I said, I'm aware this is not the case in C/C++, but I'm wondering if there's some ObjC magic here or not.
Upvotes: 10
Views: 615
Reputation: 11
Watch for local variables on this one. They don't get initialized to nil, so make sure they've been init'd somewhere before using it if you didn't init to nil in your declaration. As Peter says, class, instance and static are init'd to nil for you so no worries there.
Normally no issues, but in particular it'll make a difference in calls with pointer references passed in (like NSError **).
Upvotes: 0
Reputation: 17811
Class instance variables and static/global variables get initialized to nil/NULL/0/false as appropriate.
However local variables are not initialized.
Upvotes: 3
Reputation: 42902
They are nil, and you are allowed to assume this, for instance by testing:
if (nil == myVariableIHaventInitialisedYet)
{
myVariableIHaventInitialisedYet = [SomeThingIMightMake initWithLove];
}
Upvotes: 1
Reputation: 755131
Class instances variables in Objective-C are guaranteed to be initialized to their empty value (0, nil, etc ...)
Upvotes: 13