欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS block的值捕获与指针捕获详解

程序员文章站 2021-12-03 08:59:40
目录指针与指针变量block捕获变量方式值捕获指针捕获__block修饰的变量关于block延伸的知识点总结指针与指针变量通俗的理解:指针:内存地址指针变量:存放内存地址的变量指针变量的指针:指针变量...

指针与指针变量

通俗的理解:

指针:内存地址
指针变量:存放内存地址的变量
指针变量的指针:指针变量自身的内存地址

person *p = [person new]

iOS block的值捕获与指针捕获详解

右边isa为:对象的内存地址 - 指针

p为:指针变量

左边isa为:指针变量的内存地址 - 指针变量的指针

block捕获变量方式

对局部变量捕获有两种形式:1、值捕获(局部自动变量) 2、指针捕获(局部静态变量);全局变量无需捕获,可直接进行访问。

clang -rewrite-objc **.m -o **.cpp 不同场景下转换成c++代码结果如下(嫌代码长不想看的直接看代码下面的结论)

值捕获

指针变量的捕获

block内部用一个新的指针变量来接收原指针变量。接收后,两个指针变量里面存储的值都是对象的内存地址,所以也可以说是值的捕获。

局部自动变量:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        person *p = [person new];
        void (^block)(void) = ^{
            nslog(@"%@",p);
        };
        block();
    }
    return 0;
}
struct person_impl {
    struct nsobject_impl nsobject_ivars;
};

struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* desc;
  person *p;
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, person *_p, int flags=0) : p(_p) {
    impl.isa = &_nsconcretestackblock;
    impl.flags = flags;
    impl.funcptr = fp;
    desc = desc;
  }
};

static void __main_block_func_0(struct __main_block_impl_0 *__cself) {

  person *p = __cself->p; // bound by copy
            nslog((nsstring *)&__nsconstantstringimpl__var_folders_7w_wgxxl_655s9g6tms_7z44s6w0000gn_t_main_f76e59_mi_0,p);
}

static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_block_object_assign((void*)&dst->p, (void*)src->p, 3/*block_field_is_object*/);}

static void __main_block_dispose_0(struct __main_block_impl_0*src) {_block_object_dispose((void*)src->p, 3/*block_field_is_object*/);}

static struct __main_block_desc_0 {
  size_t reserved;
  size_t block_size;
  void (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*);
  void (*dispose)(struct __main_block_impl_0*);
} 
__main_block_desc_0_data = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0};

int main(int argc, const char * argv[]) {
    /* @autoreleasepool */ { __atautoreleasepool __autoreleasepool;
        person *p = ((person *(*)(id, sel))(void *)objc_msgsend)((id)objc_getclass("person"), sel_registername("new"));
        
        void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_data, p, 570425344));
        
        ((void (*)(__block_impl *))((__block_impl *)block)->funcptr)((__block_impl *)block);

    }
    return 0;
}

代码分析,生成的__main_block_impl_0结构体里面创建了一个指针变量p,main函数里面的__main_block_impl_0初始化时,传入的也是指针变量p。所以block对局部自动变量采用的捕获方式是指针变量的捕获,也就是值捕获。

指针捕获

对指针变量自身指针的捕获

block内部用一个新的指针来接收(指向)原指针变量自身的地址。

局部静态变量:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        static person *p = nil;
        p = [person new];
        void (^block)(void) = ^{
            nslog(@"%@",p);
        };
        block();
    }
    return 0;
}
struct person_impl {
    struct nsobject_impl nsobject_ivars;
};

struct __main_block_impl_0 {

  struct __block_impl impl;
  struct __main_block_desc_0* desc;
  person **p;
  
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, person **_p, int flags=0) : p(_p) {
    impl.isa = &_nsconcretestackblock;
    impl.flags = flags;
    impl.funcptr = fp;
    desc = desc;
  }
};

static void __main_block_func_0(struct __main_block_impl_0 *__cself) {

  person **p = __cself->p; // bound by copy
  nslog((nsstring *)&__nsconstantstringimpl__var_folders_7w_wgxxl_655s9g6tms_7z44s6w0000gn_t_main_bd39c2_mi_0,(*p));
}

static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_block_object_assign((void*)&dst->p, (void*)src->p, 3/*block_field_is_object*/);}

static void __main_block_dispose_0(struct __main_block_impl_0*src) {_block_object_dispose((void*)src->p, 3/*block_field_is_object*/);}

static struct __main_block_desc_0 {

  size_t reserved;
  size_t block_size;
  void (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*);

  void (*dispose)(struct __main_block_impl_0*);

} __main_block_desc_0_data = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0};

int main(int argc, const char * argv[]) {

    /* @autoreleasepool */ { __atautoreleasepool __autoreleasepool;

        static person *p = __null;
        p = ((person *(*)(id, sel))(void *)objc_msgsend)((id)objc_getclass("person"), sel_registername("new"));

        void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_data, &p, 570425344));
        ((void (*)(__block_impl *))((__block_impl *)block)->funcptr)((__block_impl *)block);
    }
    return 0;
}

代码分析,生成的__main_block_impl_0结构体里面创建了一个指针*p,main函数里面的__main_block_impl_0初始化时,传入的是指针变量p的地址&p。所以block对局部静态变量采用的捕获方式是指针变量自身地址的捕获,也就是指针捕获。

__block修饰的变量

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        __block person *p = [person new];
        void (^block)(void) = ^{
            nslog(@"%@",p);
        };
        block();
    }
    return 0;
}
struct person_impl {
    struct nsobject_impl nsobject_ivars;
};

struct __block_byref_p_0 {
  void *__isa;
__block_byref_p_0 *__forwarding;
 int __flags;
 int __size;
 void (*__block_byref_id_object_copy)(void*, void*);
 void (*__block_byref_id_object_dispose)(void*);
 person *p;
};

struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* desc;
  
  __block_byref_p_0 *p; // by ref
  
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __block_byref_p_0 *_p, int flags=0) : p(_p->__forwarding) {
    impl.isa = &_nsconcretestackblock;
    impl.flags = flags;
    impl.funcptr = fp;
    desc = desc;
  }
};

static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
  __block_byref_p_0 *p = __cself->p; // bound by ref
  nslog((nsstring *)&__nsconstantstringimpl__var_folders_7w_wgxxl_655s9g6tms_7z44s6w0000gn_t_main_6c171f_mi_0,(p->__forwarding->p));
}

static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_block_object_assign((void*)&dst->p, (void*)src->p, 8/*block_field_is_byref*/);}

static void __main_block_dispose_0(struct __main_block_impl_0*src) {_block_object_dispose((void*)src->p, 8/*block_field_is_byref*/);}

static struct __main_block_desc_0 {
  size_t reserved;
  size_t block_size;
  void (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*);
  void (*dispose)(struct __main_block_impl_0*);
} __main_block_desc_0_data = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0};

int main(int argc, const char * argv[]) {

    /* @autoreleasepool */ { __atautoreleasepool __autoreleasepool;
        __attribute__((__blocks__(byref))) __block_byref_p_0 p = {(void*)0,(__block_byref_p_0 *)&p, 33554432, sizeof(__block_byref_p_0), __block_byref_id_object_copy_131, __block_byref_id_object_dispose_131, ((person *(*)(id, sel))(void *)objc_msgsend)((id)objc_getclass("person"), sel_registername("new"))};

        void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_data, (__block_byref_p_0 *)&p, 570425344));

        ((void (*)(__block_impl *))((__block_impl *)block)->funcptr)((__block_impl *)block);

    }
    return 0;
}

代码分析,使用__block修饰的指针变量p,会被转换为__block_byref_p_0的结构体,结构体内持有p。main函数里面初始化__main_block_impl_0时传入的是__block_byref_p_0的地址,访问p时,通过__block_byref_p_0的__forwarding指针进行访问。其实就相当于block内部捕获了__block_byref_p_0的指针,通过指针去访问__block_byref_p_0持有的p。所以__block修饰的变量本质上也相当于是一种指针捕获,只不过不是直接捕获指针变量p的自身地址。

值捕获能否重新赋值? 进行值拷贝时,block内部同名指针变量如果执行重新赋值操作,相当于使内部的指针变量指向了一个新的对象,再对此对象进行任何操作都与原指针变量指向的原对象无关,所以不能进行重新赋值。

指针捕获能否重新赋值? block内部将block外部的指针变量的指针赋值给一个新的指针,block内部、外部的指针都指向的是同一个指针变量。如果进行赋值操作,操作的是同一个指针变量,所以可以进行重新赋值。

关于block延伸的知识点

如果文章看到了这里,相信对值捕获和指针捕获已经有了一个清晰的认识,那么可以自行思考以下几个问题,看是否真的理解了block,文章没有的答案放在评论区

  • 值捕获能否在block内被重新赋值?如果是静态变量呢?(文中已有)
  • 经__block修饰变量生成的持有变量的结构体里面__forwarding的意义在于什么?
  • 使用block有什么需要注意的点,如何去解决?

总结

到此这篇关于ios block的值捕获与指针捕获的文章就介绍到这了,更多相关ios block值捕获与指针捕获内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!