What is the difference between alloc and allocWithZone :?

From the forum discussion , it seems that the big difference is the performance factor, allocWithZone: will allocate memory from a specific memory area, which reduces the cost of exchanging.

In practice, there is almost no way to use allocWithZone: anyone can give a simple example to illustrate which case to use allocWithZone :?

Thank,

+49
memory-management objective-c
Dec 23 2018-10-12T00:
source share
6 answers

When one object creates another, its sometimes a good idea to make sure they are both allocated from the same memory area. The zone method (declared in the NSObject protocol) can be used for this purpose; This returns the zone in which the receiver is located.

This suggests that your ivars and any objects created by your classes themselves can use +allocWithZone: in such a way as to create instances that they create in the same zone.

 -(id)init { if (self = [super init]) { someIvar = [[SomeOtherClass allocWithZone:[self zone]] init]; } return self; } 
+46
Dec 23 '10 at 2:08
source share

From the Apple documentation :

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

+28
Oct 11 '13 at 7:40
source share

A good example of using allocWithZone is when you implement the NSCopy protocol, which allows you to copy your custom objects (deep copy / copy by value), for example:

 (1) ClassName *newObject = [currentObject copy]; //results in newObject being a copy of currentObject not just a reference to it 

The NSCopy protocol provides an implementation of the method:

 (2) -(id)copyWithZone:(NSZone *)zone; 

When copying an object, the message "copy" is transmitted, as described above (1), when it is specified as "copyWithZone" sends a message to method (2). otherwise you do not need to do anything to get the zone yourself.

Now that you have a β€œzone” sent to this message, you can use it to ensure that a copy is made from memory in the same region as the original.

This can be used as:

 -(id)copyWithZone:(NSZone *)zone { newCopy = [[[self class]allocWithZone:zone]init]; //gets the class of this object then allocates a new object close to this one and initialises it before returning return(newCopy); } 

This is the only place where I know that allocWithZone is actually used.

+5
Nov 18 2018-11-11T00:
source share

I am using allocWithZone in singleton. As Forrest noted, variables created from the same memory area. Thus, other classes can use or access them from the same memory zone. Save memory space when starting the application.

+2
05 Oct '12 at 3:20
source share

In the Basic Features Guide, all Zone features are now preceded by a warning that the zones will be ignored.

Zones are ignored in iOS and the OS X 64-bit runtime. You should not use zones in the current development.

 NSCreateZone NSRecycleZone NSSetZoneName NSZoneCalloc NSZoneFree NSZoneFromPointer NSZoneMalloc NSZoneName NSZoneRealloc NSDefaultMallocZone 
+2
May 27 '15 at 20:53
source share

Even if the Apple documentation states that allocWithZone:

exists for historical reasons; memory zones are no longer used by Objective-C. You should not override this method.

and

Zones are ignored in iOS and the OS X 64-bit runtime. You should not use zones in the current development.

in reality, I redefined it in the Objective-C class (in the full Objective-C project), and the method is called when I do [[Mylass alloc] init] , even if the assembly works on iPhone 6s.

But I think it is better to follow the documentation and redefine the alloc method instead, because alloc can certainly do the same job.

0
Jul 29 '16 at 10:20
source share



All Articles