Programming with C Blocks

  On Apple Devices

By Joachim Bengtsson
Edited 2009 08 02

ToC

  1. What are Blocks?
  2. Getting Started
    1. On and for Mac OS X 10.6 Snow Leopard
    2. On Mac OS X 10.5 Leopard or for iPhone
  3. Blocks in C
    1. Syntax and Usage
    2. Memory Management
  4. Blocks in Objective-C
    1. Syntax and Usage
    2. Memory Management
  5. References and Additional Sources
  6. Version History

What are Blocks?

Blocks are like functions, but written inline with the rest of your code, inside other functions. They are also called closures, because they close around variables in your scope. (They can also be called lambdas). Let me demonstrate what this means:

counter.zip
#include <stdio.h>
#include <Block.h>
typedef int (^IntBlock)();

IntBlock counter(int start, int increment) {
	__block int i = start;
	
	return Block_copy( ^ {
		int ret = i;
		i += increment;
		return ret;
	});
	
}

int main() {
	IntBlock mycounter = counter(5, 2);
	printf("First call: %d\n", mycounter());
	printf("Second call: %d\n", mycounter());
	printf("Third call: %d\n", mycounter());
	
	Block_release(mycounter);
	
	return 0;
}
/* Output:
	First call: 5
	Second call: 7
	Third call: 9
*/
			

counter is an ordinary C function that returns one of these fabled blocks. When you have a reference to one, you can call it, just as if it was a function pointer. The difference is that the block can use the variables in counter even after the call to that function has returned! The variables i and increment have become a part of the state of the block mycounter. I will go into more detail of how each part of this example works in chapter 3.

What are blocks useful for? In Ruby, they are used in place of many control structures. The standard for loop is replaced by an ordinary function taking a block:


[1, 2, 3, 4].each do |i|
	puts i
end
# outputs the numbers 1, 2, 3, 4 on a line each

each is an ordinary function that takes a block: the block is the code between the 'do' and the 'end'. This is much more powerful than one might think, because with the ability to write your own control structures, you no longer need to depend on the evolution of the language to make your code terse and readable. Functional programming has many such useful control structures, such as the map and reduce functions; the first maps each value in a list to another value, while the second reduces a list of values to a single value (e g the sum of integers in the list). This is an entire topic in itself, and I encourage to learn more about functional programming on Wikipedia as I won't be covering that anymore here.

What are blocks useful for? In Erlang, they are used as a concurrency primitive, instead of the thread.

Getting Started

On and for Mac OS X 10.6 Snow Leopard

  1. You're done! Block support is an integral part of Snow Leopard and there's nothing you need to do to use them. Even the standard library for blocks is included in libSystem, so you always automatically link with it.

TODO Okay that's true for Cocoa apps, but what about plain C apps? Non-Xcode projects?

On Mac OS X 10.5 Leopard or for iPhone

If you want to use blocks in applications targetting Mac OS 10.5 or the iPhone, and/or you're running 10.5, you need to use the third-party GCC fork Plausible Blocks. From their site:

Plausible Blocks (PLBlocks) provides a drop-in runtime and toolchain for using blocks in iPhone 2.2+ and Mac OS X 10.5 applications. Both the runtime and compiler patches are direct backports from Apple's Snow Leopard source releases.

PLBlocks is provided by Plausible Labs.

  1. Download the latest plblocks disk image for your OS from the Google Code page.
  2. Mount the disk image and run the installer. When done, keep the disk image mounted — you'll need it later.
  3. Restart Xcode if it was running, and open the project in which you want to use blocks.
  4. Open either the project-wide settings, or in the target settings (cmd-opt-E) for the specific target you want to use blocks in
  5. Search for "Compiler"
  6. From the drop-down for the setting "C/C++ Compiler Version", choose "GCC 4.2 (Plausible Blocks)" as shown in this image:
  7. On the disk image with the plblocks installer, you'll find a folder called "PLBlocks Runtime". Save this to somewhere on your disk.
  8. Now, for the target that will use blocks, you will need to link to the blocks runtime.
    • For an iPhone app, just drag the iPhone framework from the "Runtime" folder into your project and choose to link with your target.
    • For a Mac OS X 10.5 app, drag the Mac framework into your project and link with your target, and add an embed framework build phase. If you are unfamiliar with how to do this, read my guide on framework embedding (Note: You might only need to follow steps 1 through 3).

Blocks in C

Syntax and Usage

Memory Management

Blocks in Objective-C

Syntax and Usage

Memory Management

References and Additional Sources