Skip to content

Instantly share code, notes, and snippets.

@mikeash
Last active June 11, 2018 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeash/0157619487f6ed97c1b5395eff843dd5 to your computer and use it in GitHub Desktop.
Save mikeash/0157619487f6ed97c1b5395eff843dd5 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
int main(int argc, char **argv) {
auto global = ^{ NSLog(@"Hello, world!"); };
auto captures = ^{ NSLog(@"argc is %d", argc); };
NSLog(@"global: %@ - captures: %@", global, captures);
NSLog(@"(void *)global: %p - (void *)captures: %p", (void *)global, (void *)captures);
id globalCopy = [global copy];
id capturesCopy = [captures copy];
NSLog(@"[global copy]: %@ - [captures copy]: %@", globalCopy, capturesCopy);
NSLog(@"(void *)[global copy]: %p - (void *)[captures copy]: %p", (void *)globalCopy, (void *)capturesCopy);
}
// $ clang++ -framework Foundation -std=c++11 test.mm
// $ ./a.out
// 2018-06-11 09:52:50.536 a.out[16828:73422403] global: <__NSGlobalBlock__: 0x1070cd048> - captures: <__NSStackBlock__: 0x7ffee8b33988>
// 2018-06-11 09:52:50.536 a.out[16828:73422403] (void *)global: 0x1070cd048 - (void *)captures: 0x7ffee8b33988
// 2018-06-11 09:52:50.536 a.out[16828:73422403] [global copy]: <__NSGlobalBlock__: 0x1070cd048> - [captures copy]: <__NSMallocBlock__: 0x7febfb000120>
// 2018-06-11 09:52:50.536 a.out[16828:73422403] (void *)[global copy]: 0x1070cd048 - (void *)[captures copy]: 0x7febfb000120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment