fbrereto
fbrereto

Reputation: 35925

Objective-C class instance zeroed at alloc?

Is there any kind of memory-zeroing objective-c undertakes on my behalf when I first allocate a class instance? I see a lot of objective-c code out there that presumes their outlets are nil by default. If I do the same am I basing such behavior on false pretenses?

Upvotes: 4

Views: 370

Answers (2)

rpetrich
rpetrich

Reputation: 32316

NSAllocateObject returns a block of memory large enough to include all the ivars in the class; its isa pointer is set to the class, and the rest of the block is filled with 0. +[NSObject alloc] calls +[NSObject allocWithZone:] which then calls NSAllocateObject internally.

Upvotes: 8

Jon Steinmetz
Jon Steinmetz

Reputation: 4124

Yes, all alloced objects are zeroed out. See NSObject alloc documentation. It is guaranteed by the language so it is perfectly safe to do so and in fact setting the members to 0 yourself would be redundant.

Upvotes: 8

Related Questions