Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Type qualifier and friends

Felix Chern
November 08, 2012

Type qualifier and friends

C and objective-C type qualifiers

Felix Chern

November 08, 2012
Tweet

More Decks by Felix Chern

Other Decks in Programming

Transcript

  1. Daily used C/Obj-C const int * const ptr; const int

    * (* const * ptr)(float); int fun(char * restrict src, char * restrict src); UIColor* __autoreleasing red = ...; void (^block)(int volatile *, int volatile *);
  2. Daily used C/Obj-C const int * const ptr; const int

    * (* const * ptr)(float); int fun(char * restrict src, char * restrict src); UIColor* __autoreleasing red = ...; Understand standard C89 declarations void (^block)(int volatile *, int volatile *);
  3. Daily used C/Obj-C const int * const ptr; const int

    * (* const * ptr)(float); int fun(char * restrict src, char * restrict src); UIColor* __autoreleasing red = ...; C99 optimization keywords void (^block)(int volatile *, int volatile *);
  4. Daily used C/Obj-C const int * const ptr; const int

    * (* const * ptr)(float); int fun(char * restrict src, char * restrict src); void (^block)(int volatile *, int volatile *); UIColor* __autoreleasing red = ...; Lockless operations
  5. Daily used C/Obj-C const int * const ptr; const int

    * (* const * ptr)(float); int fun(char * restrict src, char * restrict src); UIColor* __autoreleasing red = ...; Objective-C ARC keywords void (^block)(int volatile *, int volatile *);
  6. int const * intPtr; Declare variable intPtr as a pointer

    to const int const int a = 4; int const * intPtr = &a; int b = 5; intPtr = &b; //compile error
  7. int * const a; // const pointer to int int

    const * const a; // const pointer to const int int const * a; // pointer to const int const int * a; // pointer to const int If no type/pointer on left, the qualifier applies to type next to it right
  8. int const arr[3]; arr [3] const int Declare arr as

    array 3 of const int int * const func(float); func (float) const * int Declare func as function(float) returning const pointer to int
  9. int const arr[3]; arr [3] const int Declare arr as

    array 3 of const int int * const func(float); func (float) const * int Declare func as function(float) returning const pointer to int
  10. int const arr[3]; arr [3] const int Declare arr as

    array 3 of const int int * const func(float); func (float) const * int Declare func as function(float) returning const pointer to int
  11. int const arr[3]; arr [3] const int Declare arr as

    array 3 of const int int * const func(float); func (float) const * int Declare func as function(float) returning const pointer to int
  12. int const arr[3]; arr [3] const int Declare arr as

    array 3 of const int int * const func(float); func (float) const * int Declare func as function(float) returning const pointer to int
  13. int const arr[3]; arr [3] const int Declare arr as

    array 3 of const int int * const func(float); func (float) const * int Declare func as function(float) returning const pointer to int
  14. int const arr[3]; arr [3] const int Declare arr as

    array 3 of const int int * const func(float); func (float) const * int Declare func as function(float) returning const pointer to int
  15. int (*const arr)[3]; arr const * [3] int Declare arr

    as const pointer to array 3 of int int (* const * func[2])(float); func [2] * const * (float) int func is array 2 of pointer to const pointer to function(float) returning int
  16. int (*const arr)[3]; arr const * [3] int Declare arr

    as const pointer to array 3 of int int (* const * func[2])(float); func [2] * const * (float) int func is array 2 of pointer to const pointer to function(float) returning int
  17. int (*const arr)[3]; arr const * [3] int Declare arr

    as const pointer to array 3 of int int (* const * func[2])(float); func [2] * const * (float) int func is array 2 of pointer to const pointer to function(float) returning int
  18. int (*const arr)[3]; arr const * [3] int Declare arr

    as const pointer to array 3 of int int (* const * func[2])(float); func [2] * const * (float) int func is array 2 of pointer to const pointer to function(float) returning int
  19. int (*const arr)[3]; arr const * [3] int Declare arr

    as const pointer to array 3 of int int (* const * func[2])(float); func [2] * const * (float) int func is array 2 of pointer to const pointer to function(float) returning int
  20. int (*const arr)[3]; arr const * [3] int Declare arr

    as const pointer to array 3 of int int (* const * func[2])(float); func [2] * const * (float) int func is array 2 of pointer to const pointer to function(float) returning int
  21. int (*const arr)[3]; arr const * [3] int Declare arr

    as const pointer to array 3 of int int (* const * func[2])(float); func [2] * const * (float) int func is array 2 of pointer to const pointer to function(float) returning int
  22. int (*const arr)[3]; arr const * [3] int Declare arr

    as const pointer to array 3 of int int (* const * func[2])(float); func [2] * const * (float) int func is array 2 of pointer to const pointer to function(float) returning int
  23. int (*const arr)[3]; arr const * [3] int Declare arr

    as const pointer to array 3 of int int (* const * func[2])(float); func [2] * const * (float) int func is array 2 of pointer to const pointer to function(float) returning int
  24. int (*const arr)[3]; arr const * [3] int Declare arr

    as const pointer to array 3 of int int (* const * func[2])(float); func [2] * const * (float) int func is array 2 of pointer to const pointer to function(float) returning int
  25. int (^(^ adder)(int))(int) adder ^(int) ^(int) int adder is a

    block(int) returning block(int) returning int Now you can read any kind of type declarations!!
  26. • const (C89) • volatile (C89) • restrict (C99) •

    __weak • __strong • __autoreleasing • __unsafe_unretained
  27. • static (C89) • auto (default, C89) • extern (C89)

    • register (not a type qualifier, C89) • __block (Clang extension) • typedef (act like storage specifier)
  28. __weak FCController * w_self = self; FCController __weak * w_self

    = self; FCController * __weak w_self = self;
  29. FCController * __weak w_self = self; self.block = ^{ FCController

    * __strong s_self = w_self; if (s_self != nil) { // self retained s_self.point = CGMakePoint(....); // blah } }; Use weak self to break retain cycle
  30. CGColorRef redRef; @autorelease{ UIColor* __autoreleasing red = [UIColor redColor]; redRef

    = CFRetain([red CGColor]); // redRef retain count: 2 } // redRef retain count: 1 CALayer *layer = [CALayer layer]; [layer setBackgroundColor redRef]; CFRelease(redRef); // redRef retain count: 0, freed Explicitly call autorelease
  31. void function (void* data) { int [] array; array =

    (int []) data; // fail } int [] is implicit int * const void function (void* data) { int * array; array = (int *) data; }