alloc 在 Objective C 中是如何工作的? [英] How does alloc work in Objective C?

查看:45
本文介绍了alloc 在 Objective C 中是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 alloc 是一个类方法,它创建类的对象并将其isa"指针指向类,这就是消息在运行时的映射方式.

I know that alloc is a class method which creates object of the class and points its "isa" pointer to the class and thats how messages are mapped at runtime.

我对 allocWithZone 和区域有一些想法.

and I have some idea about allocWithZone and zones.

谁能告诉我或指向我一个很好的链接来解释:-

Can anyone tell me or point me to a nice link which explains :-

isa 指针如何指向正确的类?

How isa pointer is pointed to the right class ?

分配了多少内存?

从父类继承的成员的内存是如何创建的?

How is memory for members inherited from parent class created ?

如果 id 是 objc_object* 的 typedef,它的 isa 指针指向什么,那么它如何保存任何对象,因为 isa 指针会将我们带到具有方法选择器的调度表,但它们是否有任何东西告诉我们什么数据成员应该在那里?

If id is a typedef for objc_object*, what does its isa pointer point to, then how does it hold anyobject because isa pointer will get us to the dispatch table which has selectors for methods but do they have anything that tells us what data-members are suppose to be there ?

推荐答案

编译器在幕后通过 objc 运行时为您插入调用.您可以在包含路径 objc/ 中找到该库.objc/runtime.h 可能是最感兴趣的.作为奖励,一些精选的常见消息会通过这些插入绕过 objc_msgSend.

The compiler inserts calls through the objc runtime for you, behind the scenes. You can find the library in your include path objc/. objc/runtime.h will probably of most interest. As a bonus, a few select common messages bypass objc_msgSend with these insertions.

isa 指针如何指向正确的类?

objc_constructInstance

分配了多少内存?

class_createInstance
class_getInstanceSize

从父类继承的成员的内存是如何创建的?

内存被清零,并且isa被设置.

The memory is zeroed, and isa is set.

如果 id 是 objc_object* 的 typedef,它的 isa 指针指向什么,那么它如何保存任何对象,因为 isa 指针会将我们带到具有方法选择器的调度表,但它们是否有任何东西告诉我们应该有哪些数据成员?

无论在初始化时设置什么.ObjC 对象指针只是原始内存.与其他语言不同,书面类型的强制转换和转换是变量地址的直接集合 - 在以下构造中没有显式的类型提升或转换:

Whatever was set at initialization. ObjC object pointers are just raw memory. Unlike other languages, casting and conversion of written types is a direct set of the variable's address - there is no explicit type promotion or conversion in memory in the following construct:

MONDisplay * display = [NSString string];
NSLog(@"%@", display);

指针与[NSString string]返回的值相同.

这篇关于alloc 在 Objective C 中是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆