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

Type qualifier and friends

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. Type Qualifiers and
    Friends
    Felix Chern (dryman)

    View Slide

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

    View Slide

  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 *);

    View Slide

  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 *);

    View Slide

  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 *);

    View Slide

  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

    View Slide

  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 *);

    View Slide

  8. C declaration

    View Slide

  9. C declaration
    is HARD!!

    View Slide

  10. Rule1:
    read from right to left

    View Slide

  11. int const * intPtr;

    View Slide

  12. int const * intPtr;
    Declare variable intPtr

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  17. Rule2:
    postfix operator () []
    precedence is higher

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  25. Rule 3:
    Surrounding
    parenthesis has greatest
    precedence

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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!!

    View Slide

  37. Type qualifiers

    View Slide

  38. • const (C89)
    • volatile (C89)
    • restrict (C99)
    • __weak
    • __strong
    • __autoreleasing
    • __unsafe_unretained

    View Slide

  39. Storage specifiers

    View Slide

  40. • static (C89)
    • auto (default, C89)
    • extern (C89)
    • register (not a type qualifier, C89)
    • __block (Clang extension)
    • typedef (act like storage specifier)

    View Slide

  41. static const int a;
    storage specifier type qualifier
    typedef const int MyInt
    typedef static int MyInt

    View Slide

  42. Objective-C
    ARC qualifiers
    • Used for explicit ARC declaration
    • Apply on objective-c pointers

    View Slide

  43. __weak FCController * w_self = self;
    FCController __weak * w_self = self;
    FCController * __weak w_self = self;

    View Slide

  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

    View Slide

  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

    View Slide

  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;
    }

    View Slide

  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

    View Slide

  48. Questions?

    View Slide