简体   繁体   English

分配+初始化内存使用机制

[英]alloc + init memory usage mechanism

I am just curious to know when an object has made by alloc , and a piece of memory allocated to it, why init don't use that piece of memory and changes the address of object? 我只是很好奇,知道何时由alloc了对象,并为其分配了一块内存,为什么init不使用该内存并更改对象的地址?

NSDate *t = nil;
NSLog(@"t = %p",t); // t = 0x0 

t = [NSDate alloc];
NSLog(@"t = %p",t); // t = 0x100107af0

t = [t init];
NSLog(@"t = %p",t); // t = 0x1001035a0

Two-step initialization allows us to do these kinds of things (namely, substituting an instance of a class for another depending on the initializer called). 两步初始化使我们可以做这些事情(即,根据所调用的初始化程序,将一个类的实例替换为另一个实例)。 Class clusters all over Foundation and UIKit take advantage of this to return instances optimized for a particular use-case. Foundation和UIKit上的整个类集群都利用这一点来返回针对特定用例优化的实例。 For example, UIColor itself is just an interface for its subclasses that implement color caching (all the named initializers like +blackColor ), the RGB color space ( +colorWithRed:green:blue:alpha ), the black and white color space ( +colorWithWhite:alpha: ), CIColor compatibility, etc. And so NSDate is too. 例如, UIColor本身只是其子类的接口,这些子类实现了颜色缓存(所有已命名的初始化方法,如+blackColor ),RGB颜色空间( +colorWithRed:green:blue:alpha ),黑白颜色空间( +colorWithWhite:alpha: ), CIColor兼容性等等等NSDate过。 When you call -init , Apple has means and motive to return a different object that implements the same interface as NSDate as an optimization because you honestly shouldn't care what you get as long as it doesn't launch the missiles when you try to message it properly. 当您调用-init ,Apple有手段和动机返回实现与NSDate相同的接口作为优化的另一个对象,因为说实话,只要您不尝试发射任何东西,只要它不发射导弹,您就不会在意正确发送消息。

As of the latest iOS SDK, calling [NSDate alloc] always returns the same memory location. 从最新的iOS SDK开始,调用[NSDate alloc]始终返回相同的内存位置。 You can verify this behavior with the following code: 您可以使用以下代码验证此行为:

NSDate *date1, *date2;
date1 = [NSDate alloc];
NSLog(@"date1: %p", date1);
date1 = [date1 init];
NSLog(@"date1: %p", date1);
date2 = [NSDate alloc];
NSLog(@"date2: %p", date2);
date2 = [date2 init];
NSLog(@"date2: %p", date2);

I suspect that it has to do with the fact that NSDate is a class cluster . 我怀疑这与NSDate是一个类集群这一事实有关。

If the private subclasses of a class cluster have different storage requirements, it's impossible to know inside of alloc how much memory to allocate. 如果类群集的私有子类具有不同的存储要求,则不可能在alloc内部知道要分配多少内存。 One approach to solving this problem, and it appears this is the approach that Apple is using with NSDate , is to let the init and factory methods handle all the memory allocation, since those methods know what private subclass is actually going to be used. 解决此问题的一种方法,看来这是Apple与NSDate使用的方法,是让init和factory方法处理所有内存分配,因为这些方法知道实际上将使用什么私有子类。

At that point, all alloc is doing for you is allowing the user to preserve the [[NSDate alloc] init] pattern that's used for object creation everywhere in Objective-C. 到那时,所有alloc都为您做着,就是允许用户保留用于[[NSDate alloc] init] Objective-C中各处创建对象的[[NSDate alloc] init]模式[[NSDate alloc] init] Since the memory location returned by alloc is always discarded, alloc may as well just return a fixed memory location, which is what it appears to be doing. 由于alloc返回的内存位置始终被丢弃,因此alloc也可能只返回固定的内存位置,这似乎是在做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM