Carpe diem (Felix's blog)

I am a happy developer

Installing swank.js

People say that emacs is the most extendable editor in the world; however, I didn’t really understand how powerful it is until one of it extension Swank.js blowed my mind. Take a look at this awesome screen cast produced by emacs rocks.

Deep C: Understanding the Design of C Integer Types

C is a popular language designed for cross platform development. However, when you dig deeper and deeper, you might get confused for the ambiguity of C integer types. Take char for example, number of bits can be 8, 9, or more; the minimum of a signed char is not strictly defined as -128 but -127 or less.

name express value
CHAR_BIT Number of bits for a char object (byte) 8 or greater
SCHAR_MIN Minimum value for an object of type signed char -127 or less
SCHAR_MAX Maximum value for an object of type signed char 127 or more
UChar_MAX Maximum value for an object of type unsigned char 255 or more

Why does C is designed like so? In this article I’ll discuss the design and the sprits of C.

Objective-C ARC Common Pitfalls and Best Practices

Objective-C is a really cool programming language that is designed for Mac OSX and iOS software development. TIOBE has announced November Haedline: Objective-C on its way to become “language of the year” again. It is popular not only because the platform, but also its great performance on mobile devices. Objective-C featured in its manually memory management instead of garbage collection. Yet, its not that manual in modern Objective-C. Apple introduced Automatic Reference Counting, ARC, that inserts memory management code for you on compile time. In most cases of Objective-C development, it JUST WORKS. However, it is often confusing when you mix ARC code with Core Foundation objects (low level C references on apple’s platform). Today, I’ll talk about pitfalls and concepts of ARC especially when gluing CF objects with toll-free bridging.

Deep C: Integer Promotion

Almost every programmer has learned about C, and a lot of them use it for their career. It is certainly one of the most popular programming languages on TIOBE (first place in November 2012). Yet, C can be really tricky and behave unexpectedly sometimes. One of those dodgy side of C is integer promotion. See the following example that illustrate the issue:

1
2
3
4
5
6
7
8
9
#include <stdio.h>

int main(void)
{
    unsigned char a = 0xff;
    char b = 0xff;
    int c = a==b; // true, or false?
    printf("C: %d\n",c);
}

You might think the output is 1, yet the answer is 0. Oops.

Dance With Objective-C Dynamic Types

Objective-C is a super set of C language. The entire language is a preprocessor skin added to C language and a powerful runtime system. With this runtime system, one can have full featured object oriented programming interface, functional programming environment, and magical dynamic typing system.

In this post, I’ll go through common tasks you can do with Objective-C typing system, including querying normal NSObject types, packing static type with NSValue, testing core foundation references, and validating if a pointer is a valid object pointer.

Type Qualifiers and Friends

Type qualifiers are heavily used in C and Objective C. In C99 there are three type qualifiers: const, restrict, and volatile. In objective C, Apple introduced __weak, __strong, __unsafe_unretained, and __autoreleasing for automatic reference counting.

It is easy to get confused with complicated type qualifiers. For example:

1
char* const * x;  // x is a pointer to const pointer to char

In this post I’ll go through what type qualifiers are, and how do we read and write it in correct way.

C/ObjC Block Byref Internals

In the last post, I mentioned that __block variable (here we named it block byref) will be retained if multiple blocks referenced it. Here are some sample code to show how runtime deals with reference counts.

C/ObjC Block Quizzes

Apple introduced blocks (anonymous functions or lambdas) as C extensions for its parallel programming model Grand Central Dispatch. Unlike ordinary C functions, blocks can capture surrounding variable contexts. The captured variables are casts to const by default, and for mutable variables you can mark it with __block storage qualifier. However, there is a lot of pitfalls in __block variables. Can you identify all of them?