Adventures in the transition from C to Cocoa.

Thursday, June 14, 2007

A Crash Course in Cocoa

I've been a C/C++ programmer for years. While it wasn't my first programming language, I have certainly used it the most and spent the most time with it. To further complicate matters, many of the other languages I've worked with over the past several years that weren't C or C++ were at least strongly based on C's syntax and structure. With all of this experience and momentum, it is personally difficult to change my ways and move on to new and different things.

But all is not lost! While traveling blindly on this path from C to Cocoa, I intend to document my findings, annoyances, and cool tricks for posterity. May your journey be easier where trails have already been blazed.


Cocoa is a superset of the C programming language. While it has some similarities with C++, it uses none of its conventions, and certainly none of its syntax. There are parallels, however. It's simply a matter of finding them.

The simplest part of the language, its datatypes, are textbook C types. You can use this program to find the sizes of various types:

main.m:

#include <stdio.h>

#define pso(type) \
printf("sizeof "#type": %4i bits\n",sizeof(type)*8);

int main(void)
{
pso(unsigned char);
pso( signed char);
pso(unsigned short int);
pso(short int);
pso(unsigned int);
pso(int);
pso(unsigned long);
pso(long);
pso(float);
pso(double);
pso(long double);
pso(void*);
return 0;
}


Compiled using gcc main.m -o main. As you can see, the code is perfectly valid C, minus the macro hackery for simplicity. POSIX is even thrown in for good measure. To compile the program for 64-bit mode, use this: gcc main.m -o main64 -m64. The -m64 part tells GCC to use 64-bit instructions and addressing. Currently, this only works for command-line applications under OS X 10.4, but for 10.5 it should work for GUI applications as well.

The output of this program is also comforting. For 32-bit mode, the output looks something like this:

sizeof unsigned char:       8 bits
sizeof signed char: 8 bits
sizeof unsigned short int: 16 bits
sizeof short int: 16 bits
sizeof unsigned int: 32 bits
sizeof int: 32 bits
sizeof unsigned long: 32 bits
sizeof long: 32 bits
sizeof float: 32 bits
sizeof double: 64 bits
sizeof long double: 128 bits
sizeof void*: 32 bits


The output for 64-bit builds looks like this:
sizeof unsigned char:       8 bits
sizeof signed char: 8 bits
sizeof unsigned short int: 16 bits
sizeof short int: 16 bits
sizeof unsigned int: 32 bits
sizeof int: 32 bits
sizeof unsigned long: 64 bits
sizeof long: 64 bits
sizeof float: 32 bits
sizeof double: 64 bits
sizeof long double: 128 bits
sizeof void*: 64 bits


The only differences are the length of long and void*. The latter means that pointers are 64 bits instead of 32, giving us address space far beyond the 4GB limit. It also means that when doing pointer arithmetic, using int types will result in errors when the top 32 bits get truncated.

So from the above, we've seen that Cocoa can look and act exactly like C. The data types are the same, as is the syntax. The ride only gets bumpier from here though. With the introduction of Objects, we'll see both data types and syntax get turned upside-down.

No comments:

Categories