Type qualifier and friends

Type qualifier and friends

C and objective-C type qualifiers

Cba7f423f9e0ee3a0be1ca18978a6684?s=128

Felix Chern

November 08, 2012
Tweet

Transcript

  1. Type Qualifiers and Friends Felix Chern (dryman)

  2. Felix Chern (dryman) iOS developer github: idryman twitter: @idryman blog:

    idryman.org
  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 = ...; 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); UIColor* __autoreleasing red = ...; Understand standard C89 declarations void (^block)(int volatile *, int volatile *);
  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 = ...; C99 optimization keywords void (^block)(int volatile *, int volatile *);
  6. 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
  7. 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 *);
  8. C declaration

  9. C declaration is HARD!!

  10. Rule1: read from right to left

  11. int const * intPtr;

  12. int const * intPtr; Declare variable intPtr

  13. int const * intPtr; Declare variable intPtr as a pointer

    to
  14. int const * intPtr; Declare variable intPtr as a pointer

    to const int
  15. 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
  16. 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
  17. Rule2: postfix operator () [] precedence is higher

  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. Rule 3: Surrounding parenthesis has greatest precedence

  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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!!
  37. Type qualifiers

  38. • const (C89) • volatile (C89) • restrict (C99) •

    __weak • __strong • __autoreleasing • __unsafe_unretained
  39. Storage specifiers

  40. • static (C89) • auto (default, C89) • extern (C89)

    • register (not a type qualifier, C89) • __block (Clang extension) • typedef (act like storage specifier)
  41. static const int a; storage specifier type qualifier typedef const

    int MyInt typedef static int MyInt
  42. Objective-C ARC qualifiers • Used for explicit ARC declaration •

    Apply on objective-c pointers
  43. __weak FCController * w_self = self; FCController __weak * w_self

    = self; FCController * __weak w_self = self;
  44. 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
  45. 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
  46. void function (void* data) { int [] array; array =

    (int []) data; // fail } int [] is implicit int * const void function (void* data) { int * array; array = (int *) data; }
  47. See also • http://www.idryman.org/blog/2012/10/29/type-qualifiers/ • http://www.vineetgupta.com/2011/03/deciphering-complex-c-declarations/ • http://cdecl.org volatile, restrict

  48. Questions?