Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is difference between alloc and allocWithZone:?

From forum discussion , seem like that the big difference is performance factor, allocWithZone: will alloc memory from particular memory area, which reduce cost of swapping.

In practice, almost get no chance to use allocWithZone: , anyone can give simple example to illustrate which case to use allocWithZone: ?

Thanks,

like image 923
Forrest Avatar asked Dec 23 '10 02:12

Forrest


2 Answers

When one object creates another, it’s sometimes a good idea to make sure they’re both allocated from the same region of memory. The zone method (declared in the NSObject protocol) can be used for this purpose; it returns the zone where the receiver is located.

This suggests to me that your ivars, and any objects your classes "create" themselves could make use of +allocWithZone: in this way, to make the instances they create in the same zone.

-(id)init {   if (self = [super init]) {     someIvar = [[SomeOtherClass allocWithZone:[self zone]] init];   }    return self; } 
like image 80
d11wtq Avatar answered Sep 19 '22 03:09

d11wtq


From Apple's documentation:

This method exists for historical reasons; memory zones are no longer used by Objective-C.

like image 30
Rivera Avatar answered Sep 19 '22 03:09

Rivera