objective c什么时候用二维指针
When should we use two-dimensional pointer in objective c
我想知道objective c什么时候应该使用二维指针。我看了一篇关于运行时机制的文章。方法objc_msgSend的实现细节如下:
任何 NSObject
objective 都有一个 isa
的属性,它将指向相应的 Class
对象。
@interface NSObject <NSObject> {
Class isa OBJC_ISA_AVAILABILITY;
}
Class
objective如下:
struct objc_class {
Class isa;
Class superclass;
const char *name;
uint32_t version;
uint32_t info;
uint32_t instance_size;
struct old_ivar_list *ivars;
struct old_method_list **methodLists; // Method list of the class
Cache cache;
struct old_protocol_list *protocols;
}
我想问的问题是为什么methodLists是二维指针,如果用一维或者不用指针呢,能不能解释一下这个问题?先谢谢了。
结构old_method_list如下:
struct old_method_list {
void *obsolete;
int method_count;
/* variable length structure */
struct old_method method_list[1]; //The address of the first Method
};
好吧,我看了另一篇关于为什么old_method_list使用二维指针的文章,原因是,它可能指向一个array.My另一个问题是对于struct old_method method_list[1]
,注释是"The address of the first Method",但是method_list是一个长度为1的old_method数组。它如何存储地址?
我通过阅读另一篇文章解决了这个问题。
数组结构 old_method method_list[1] 是动态的,可以通过向其添加元素(方法)来更改它。
因为它指向 old_method_list
处的指针数组。
更新old_method_list
。
old_method_list *
不仅可以指向 old_method_list
。它还可以指向例如:
struct old_method_list_with_10_methods
{
struct old_method_list list;
struct old_method method_list[9];
};
或者如果您需要动态尺寸:
old_method_list* list = malloc(sizeof(old_method_list) + (n-1) * sizeof(old_method));
list->method_count = n;
就是这样的变长结构
我想知道objective c什么时候应该使用二维指针。我看了一篇关于运行时机制的文章。方法objc_msgSend的实现细节如下:
任何
NSObject
objective 都有一个isa
的属性,它将指向相应的Class
对象。@interface NSObject <NSObject> { Class isa OBJC_ISA_AVAILABILITY; }
Class
objective如下:struct objc_class { Class isa; Class superclass; const char *name; uint32_t version; uint32_t info; uint32_t instance_size; struct old_ivar_list *ivars; struct old_method_list **methodLists; // Method list of the class Cache cache; struct old_protocol_list *protocols; }
我想问的问题是为什么methodLists是二维指针,如果用一维或者不用指针呢,能不能解释一下这个问题?先谢谢了。
结构old_method_list如下:
struct old_method_list { void *obsolete; int method_count; /* variable length structure */ struct old_method method_list[1]; //The address of the first Method };
好吧,我看了另一篇关于为什么old_method_list使用二维指针的文章,原因是,它可能指向一个array.My另一个问题是对于
struct old_method method_list[1]
,注释是"The address of the first Method",但是method_list是一个长度为1的old_method数组。它如何存储地址?
我通过阅读另一篇文章解决了这个问题。 数组结构 old_method method_list[1] 是动态的,可以通过向其添加元素(方法)来更改它。
因为它指向 old_method_list
处的指针数组。
更新old_method_list
。
old_method_list *
不仅可以指向 old_method_list
。它还可以指向例如:
struct old_method_list_with_10_methods
{
struct old_method_list list;
struct old_method method_list[9];
};
或者如果您需要动态尺寸:
old_method_list* list = malloc(sizeof(old_method_list) + (n-1) * sizeof(old_method));
list->method_count = n;
就是这样的变长结构