令人惊讶的失败将C数组放入Objective-C结构属性中

ren*_*sch 5 arrays properties objective-c ivar

我知道Objective-C不允许您使用C数组作为属性类型.在这种情况下,我得到了我期望的编译器错误.

但我对结构属性中的C数组的行为感到惊讶:

  • 没有编译错误或警告.
  • 地址本身的意外地址(gdb info malloc不知道它,不确定它是否是未初始化的内存或什么.但我会发现崩溃或显然工作,虽然内存损坏).
  • 作业成为无操作.

我把它煮成了这个示例代码:

#import <Foundation/Foundation.h>

#ifndef sizeofA
    #define sizeofA(array) (sizeof(array)/sizeof(array[0]))
#endif

@interface IncludeCArrayDirectly : NSObject // Doesn't even compile
// Uncomment below to see the compilation error for yourself.
//@property(nonatomic, assign) int8_t f[9]; // ERROR: Property cannot have array or function type 'int8_t [9]'
@end

@interface IncludeCArrayInStruct : NSObject // Compiles (no warning) and runs but is amazingly broken.
@property(nonatomic, assign) int normalProperty;
@property(nonatomic, assign) struct { int f[9]; } p;
- (void*)normalPropertysAddress;
@end

@interface IncludeCArrayInIvar : NSObject {  // Totally works.
    @public
    int normalIvar;
    int8_t f[9];
}
@end

int main(int argc, const char *argv[]) {
    @autoreleasepool {
        {{
            IncludeCArrayInStruct *a = [IncludeCArrayInStruct new];

            // Notice a.p.f's address is off in 0x7fffxxxx-land:
            printf("&a = %p, &a.normalProperty = %p, a.p.f = %p\n",
                   a, [a normalPropertysAddress], a.p.f);

            printf("a.p.f[4] BEFORE %d\n", a.p.f[4]);
            a.p.f[4] = 42;
            printf("a.p.f[4] AFTER %d\n", a.p.f[4]);
            assert(a.p.f[4] == 0); // Surprise! Assertion passes. Assignment above is a no-op.

            // Dump all of a.p.f just to take a better look:
            for (unsigned i = 0; i < sizeofA(a.p.f); i++) {
                printf("a.p.f[%d] == %d\n", i, a.p.f[i]);
            }
        }}
        {{
            IncludeCArrayInIvar *b = [IncludeCArrayInIvar new];

            // All these addresses are about what you'd expect:
            printf("&b = %p, &b.normalIvar = %p, b.f = %p\n",
                   b, &b->normalIvar, b->f);

            printf("b->f[4] BEFORE %d\n", b->f[4]);
            b->f[4] = 42;
            printf("a->f[4] AFTER %d\n", b->f[4]);
            assert(b->f[4] == 42); // No surprise here, above assignment worked.

            // Dump all of b.f just to take a better look:
            for (unsigned i = 0; i < sizeofA(b->f); i++) {
                printf("b->f[%d] == %d\n", i, b->f[i]);
            }
        }}

    }
    return 0;
}


@implementation IncludeCArrayDirectly
@end

@implementation IncludeCArrayInStruct
- (void*)normalPropertysAddress {
    return &_normalProperty;
}
@end

@implementation IncludeCArrayInIvar
@end
Run Code Online (Sandbox Code Playgroud)

对我的谜题的任何解释都在上面?

dre*_*lax 9

struct对象始终按值复制,而不是按引用复制.这意味着,当你struct通过一个访问方法返回,即返回的对象是一个副本的一个在你的对象实例.我怀疑它来自C,它在独立函数的场景中也没有区别,它也共享该返回类型:

struct sample
{
    int arr[4];
};

struct sample FunctionThatReturnsSample(void)
{
    static struct sample s = { { 0, 1, 2, 3 } };
    return s;
}

int main(void)
{
    FunctionThatReturnsSample().arr[3] = 4;

    printf("%d\n", FunctionThatReturnsSample().arr[3]);
    // still prints "3"
}
Run Code Online (Sandbox Code Playgroud)