zoukankan      html  css  js  c++  java
  • 带__Block和不带的差别

    带了__block

    #pragma clang assume_nonnull end
    
    struct __Block_byref_i_0 {
      void *__isa;
    __Block_byref_i_0 *__forwarding;
     int __flags;
     int __size;
     int i;
    };
    
    struct __main_block_impl_0 {
      struct __block_impl impl;
      struct __main_block_desc_0* Desc;
      __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int flags=0) {
        impl.isa = &_NSConcreteStackBlock;
        impl.Flags = flags;
        impl.FuncPtr = fp;
        Desc = desc;
      }
    };
    static int __main_block_func_0(struct __main_block_impl_0 *__cself, int index) {
    
            index += 1;
            printf("====> %d
    ",index);
            return index;
        }
    
    static struct __main_block_desc_0 {
      size_t reserved;
      size_t Block_size;
    } __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0)};
    int main () {
        __Block_byref_i_0 i = {(void*)0,(__Block_byref_i_0 *)&i, 0, sizeof(__Block_byref_i_0), 10};
        int (*testBlk)(int) = ((int (*)(int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
        printf("---> %d
    ",((int (*)(__block_impl *, int))((__block_impl *)testBlk)->FuncPtr)((__block_impl *)testBlk, (i.__forwarding->i)));
        return 0;
    }
    #import <Foundation/Foundation.h>
    
    int main () {
        int __block i = 10;
        int (^testBlk)(int) = ^int(int index){
            index += 1;
            printf("====> %d
    ",index);
            return index;
        };
        printf("---> %d
    ",testBlk(i));
        return 0;
    }

    没有带__block

    struct __main_block_impl_0 {
      struct __block_impl impl;
      struct __main_block_desc_0* Desc;
      __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int flags=0) {
        impl.isa = &_NSConcreteStackBlock;
        impl.Flags = flags;
        impl.FuncPtr = fp;
        Desc = desc;
      }
    };
    static int __main_block_func_0(struct __main_block_impl_0 *__cself, int index) {
    
            int addIndex = index + 1;
            printf("====> %d
    ",addIndex);
            return addIndex;
        }
    
    static struct __main_block_desc_0 {
      size_t reserved;
      size_t Block_size;
    } __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0)};
    int main () {
        int i = 10;
        int (*testBlk)(int) = ((int (*)(int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
        printf("---> %d
    ",((int (*)(__block_impl *, int))((__block_impl *)testBlk)->FuncPtr)((__block_impl *)testBlk, i));
        return 0;
    }
    #import <Foundation/Foundation.h>
    
    int main () {
        int i = 10;
        int (^testBlk)(int) = ^int(int index){
            int addIndex = index + 1;
            printf("====> %d
    ",addIndex);
            return addIndex;
        };
        printf("---> %d
    ",testBlk(i));
        return 0;
    }
  • 相关阅读:
    设置函数环境——setfenv(转)
    全局变量声明的规范化(转)
    利用__index和__newindex实现默认值表、监控表、只读表(转)
    php中的$_GET怎样获取带有井号“#”的參数
    Servlet配置load-on-startup
    LinQ—扩展方法
    CRT
    [C++] 获取IE代理server的账号password
    一步一步写算法(之hash表)
    android之PackageManager简单介绍
  • 原文地址:https://www.cnblogs.com/yuxiaoyiyou/p/11282221.html
Copyright © 2011-2022 走看看