[cfe-dev] Objective-C return array type extension?

Chen Xiliang xlchen1291 at gmail.com
Fri Jun 14 03:03:11 PDT 2013


Hi all,

I recently discovered that I can use a fixed size array as a return type in Objective-C like this:

+ (int[2])getIntArray {
    int i = 1;
    int i2 = 2;
    return (int[2]) {i, i2};
}

It woks as exactly as I expected in 32bits build (iOS device, iOS simulator, 32bits OSX app) but it failed badly in 64bits build.

>From the disassembly for 32bits build, I found that it is using _objc_msgSend_stret to send the message so the whole array is copied and returned.
>From the disassembly for 64bits build, it is using _objc_msgSend to send the message so only a pointer is returned.

I wonder is this some undocumented extension or just undefined behavior that happened to be working?

A working code is in this link:https://gist.github.com/xlc/5780693

It execute without fail any assertion using 32bits build, but failed in 64bits build.

clang -v
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.3.0
Thread model: posix

Thanks,
Bryan Chen



More information about the cfe-dev mailing list