Beware of struct properties

January 1, 2009

Objective-C is an interesting programming language. Unlike C++, it’s a strict superset of C — every valid C program is a valid Objective-C program. While most C programs are valid C++ programs, there are certain incompatibilities such as the introduction of new keywords that make the relationship not strict.

One of the many features that Objective-C brings is properties. Properties essentially act like public member variables, but whenever you get or set them, those gets and sets get rerouted through a function call (more specifically, the passing of a message in Objective-C parlance). For example, if value is a property of whatever class my_object belongs to, then the code

int x = my_object.value;

gets converted into

int x = [my_object value];  // pass the 'value' message to my_object

And this code

my_object.value = 3;

gets converted into

// pass the 'setValue' message with parameter 3 to my_object
[my_object setValue:3];

Getters and setters allow you to do things like parameter validation, logging, or anything else you want whenever a property is read from or written to. The dot notation is just syntactic sugar.

Now suppose that value isn’t a simple integer, but rather a struct of some sort, such as a CGPoint. A CGPoint is a standard 2D point class, containing two float members named x and y. Consider the following innocent-looking code:

my_object.value.x = 3;  // set x-coordinate to 3?

If value were just a plain ordinary struct member, this would do exactly what you’d expect – set the x-coordinate to 3, and leave the y-coordinate untouched. But as you’ve probably surmised by now, that’s not what happens when value is a property. Why not? Well, since we’re not assigning to it, it gets turned into this:

[my_object value].x = 3;

And [my_object value] returns an object of type CGPoint. And structs are returned… value. Thus, we’re assigning 3 to the x-coordinate of a temporary CGPoint which is a copy of our object’s value property. The only proper way to resolve this is to make value a regular class member and not a property, reassign a completely new CGPoint to value, or add messages that allow you to independently set the x- and y-coordinates. Depending on whether or not you own the code for the class, this may or not be possible.

Fortunately, in the case of CGPoints, it’s relatively easy to assign a new value with CGPointMake():

my_object.value = CGPointMake(3, my_object.value.y);

But for more complicated structs, this solution could be less than ideal.

Comments Off on Beware of struct properties

Not your average regex find-and-replace

December 25, 2008

I was reading through some of the Coding Horror archives, and came across this post examining Microsoft Visual Studio’s use of regular expressions in its find-and-replace dialog. Visual Studio has support for all of the usual regex constructs but throws in a lot of its own quirky constructs too. I suppose they could be useful if you learned to use them.

But this post isn’t about Visual Studio. Jeff’s post contained an intriguing link to Steve Yegge’s blog, where Steve goes over some of the new features in Emacs 22. And despite my being a long-time emacs user, I was pleasantly surprised to learn that you can use elisp with find-and-replace.

Now at first, this may not seem like a big deal. You can do a lot with a plain vanilla find-and-replace (M-x replace-string and M-x query-replace (also bound to M-%)), and regex find-and-replaces (M-x replace-regexp and M-x query-replace-regexp (also bound to C-M-%)) are damn powerful. But sometimes, when a sed script won’t do, an elisp find-and-replace can save you a lot of pain.

To do an elisp find-and-replace, do a regular replace-regexp, and set the replacement expression to \, (backslash-comma) followed by an elisp expression. You can use backreferences \1 through \9 as string variables, and you can use \# as an integer variable that is the total number of replacements made so far.

Steve has a number of great examples which I’m not going to repeat here, so I encourage you to go over there and eat ’em up.

I was eager to try them out, and seeing as how Emacs 22 is over two and half years old, I figured my current Emacs install would be up-to-date. Not so – my MacBook was still running Emacs 21.2.1, which came installed when I bought it in 2006. Fortunately, with MacPorts installed (HIGHLY recommended for anyone running OS X), upgrading was almost as easy as running

sudo port install emacs

I did install instead of upgrade because my emacs install was the default one that came installed and I hadn’t migrated it into MacPorts yet. I said it was almost that easy because after it installed and I went to run it, the old Emacs 21 ran instead, even though the MacPorts binaries directory /opt/local/bin precedes /usr/bin in my $PATH. It turns out that bash uses a hash table, of which I was previously unaware, to cache where frequently used commands are located to avoid searching your $PATH frequently. To fix this, just run hash -d emacs to delete its binding for emacs (or hash -r to clear the entire table), and you’re good to go!

Comments Off on Not your average regex find-and-replace

The Infamous Red Ring of Death

November 28, 2008

About two and half weeks ago, my Xbox 360 gave me a Red Ring of Death, indicative of a general hardware failure.

Red Ring of Death

It’s about two years old now, and there weren’t really any warning signs.  I went to turn it on, and….it didn’t.  Most RRoDs are caused by overheating; I generally keep my 360 well-ventilated, and I definitely don’t leave it running for long periods of time when I’m not playing it.  I’m going to chalk this one up to poor hardware design and workmanship.

I immediately called 1-800-4MYXBOX.  Boy were they unhelpful.  The automated voice-activated menus were a pain to work through, and I had to repeat myself several times to get what I wanted.  Why can’t we just have a plain old “Press 1″ instead of say “Three flashing red lights”?  It’s easier for everyone.

After wasting my time with that, I instead went to and had a much easier time.  It took me less than five minutes to file a support ticket and print out a pre-paid shipping label (I also had the option of getting a box shipped to me, but that would have taken longer obviously).  No human interaction required.

The next morning I brought my 360 down to the UPS Store and had it shipped off to the repair center in Mesquite, Texas.  It took about a week to get there, a couple days to get fixed, and another week to come home.

Now it’s back to Mega Man 9!  I’ve completed all 12/12 achievements (200 points) and 34/38 challenges.  All I have left are “Beat the game without getting hit”, “beat the game while picking up only 8 energy capsules”, “beat the game 30 times”, and “beat every boss with 1 hit point left”.

The Green Ring of Life is back, but for how long?


Slashdot Easter Egg

October 11, 2008

Did you know that every time you view a page on slashdot, you’re given a random Futurama quote? Most people don’t, since they’re hidden away in the HTTP headers, so you’ll never see them via normal web browsing. I discovered this one time when I was playing around with cURL. Here’s a sample HTTP trace:

adam-rosenfields-computer ~$ curl -I
HTTP/1.1 200 OK
Date: Sat, 11 Oct 2008 04:16:23 GMT
Server: Apache/1.3.41 (Unix) mod_perl/1.31-rc4
X-Powered-By: Slash 2.005001224
X-Bender: Boy, were we suckers!
Cache-Control: private
Pragma: private
Connection: close
Content-Type: text/html; charset=iso-8859-1

-I is the option for doing a HEAD request instead of a normal GET. So, a quick and dirty script to generate a random Futurama quote is:

curl -I 2>/dev/null | grep '^X-' | grep -v '^X-Powered-By'

This works since all of the quotes are under a header name beginning with X-, e.g. X-Bender, X-Fry, or X-Leela. I’m not sure how many total quotes there are in the database, but you could probably get a good estimate by sampling a large number of times and counting how many unique quotes and how many duplicates you get, and then extrapolating.

Comments Off on Slashdot Easter Egg

Even Sony uses Free Software

September 28, 2008

I just purchased a new 46″ HDTV, the Sony Bravia KDL-46V4100, and boy is it sweet. But I’m not here today to advertise TVs to you. I was reading through the various documentation that came in the box, and I came across a copy of the GNU General Public License and the Lesser General Public License. Even Sony uses Free Software in their televisions.

The 46V4100 includes the following GPL’ed software: Linux Kernel (no version number mentioned), busybox, and pump (no idea what this is). Under the LGPL, it contains glibc. Other software listed includes Independent JPEG Group Software, Software Developed by the OpenSSL Project for Use in the SSL-Toolkit (SSLeay License), Freetype2, Expat, cURL, and Popt.

Comments Off on Even Sony uses Free Software

Brothers in Arms: Double Time

September 26, 2008

After a long wait, Brothers in Arms: Double Time finally came out this past Tuesday on the Wii. I’m not really a fan of war games or of first person shooters, so why do I care? Well, it’s my first game credit — I was a developer on it during the summer of 2007 when I was an intern at Demiurge Studios. It sure is exciting to see a game on sale on GameStop that, when you go to watch the credits, has my name in it.

Brothers in Arms: Double Time box art

And if you do so, you’ll notice that the programming team at Demiurge is listed alphabetically by first name, except for me. I was added to the project midway through development. The credits had already been (mostly) done before I joined the team, and then they slapped my name onto the end of the list; nobody ever bothered to resort it afterwards. Was it intern bias? Nah, I think they just didn’t think to resort it.

Double Time is a port of the two games Brothers in Arms: Road to Hill 30 and Brothers in Arms: Earned in Blood to the Wii. The two games were originally made by Gearbox Software for the Xbox in 2005; they had been previously ported to the PS2 and Windows. Gearbox contracted Demiurge to do the Wii ports. If you’re into war games, you’d probably enjoy Double Time, since it’s not your typical run-and-gun shooter — you (gasp) have to use tactics to beat it.

You should all go down to your friendly neighborhood video game store and buy Brothers in Arms: Double Time! Two great games for the low price of $50!

Comments Off on Brothers in Arms: Double Time

Mega Man 9

September 23, 2008

Mega Man 9 just came out yesterday for the Wii Virtual Console. It’s an old-school 2D platformer in the style of the original Mega Man series (MM1-6), with 8-bit graphics and sounds. The developers even made some promotional box art, reminiscent of the original Mega Man box art. Unfortunately, since the game can only be downloaded, only a select few will be able to get their hands on the boxes.

Mega Man 9 will be coming out for the Xbox Live Arcade and the PlayStation Network in the near future; I’m going to wait until it does so to get it. Not only is the Xbox 360 controller far superior to the Wiimote for platformers, but the XBLA version comes with some of the most hardcore achievements of almost any Xbox 360 title to date.

Being an XBLA title, it’s already at a disadvantage compared to regular Xbox 360 games — only 200 achievement points instead of 1000. In most games, you can often get about 1/4 to 1/2 of the achievement points by playing through the game and beating it once. Some games, though, especially the ones that came out early in the 360’s life, are pathetically easy – if you pick the right games, you can get 6000 points without really trying.

But Mega Man 9 is different. Beat the game, and you get a paltry 5 points out of 200. Scoring all 200 points is worth some serious bragging rights, but you’re playing the wrong game if you’re a Gamerscore whore.

Comments Off on Mega Man 9

Digital Restrictions Management

September 12, 2008

A topic that has come under a lot of scrutiny recently is that of Digital Rights Management (DRM) in PC games, also referred to by some as Digital Restrictions Management. The most prominent titles featuring DRM are BioShock, Mass Effect, and, most recently, Spore.

The sad thing is, DRM hinders the legitimate users without stopping the pirates at all. In the case of Spore, the DRM was cracked and the game was torrented a day before it released. Countless people have complained over the overly Draconian policies, such as you’re only allowed to install the game a total of 3 times anywhere (even if you uninstall it), and you have to have the CD-ROM in the drive to play.

The fan backlash to this has been incredible. So incredible, in fact, that over 2000 people have given Spore a review of 1 star out of 5 on Amazon. As of this writing, 2005 out of 2202 reviews are 1 star.

Spore reviews on

I hope this really sends a message to EA: we don’t want Digital Restrictions Management, and we’re going to hit you in the wallet for it. A small number of people boycotting the game for DRM will hardly make a dent in EA’s bottom lime, but a loud presence on Amazon certainly will. I, for one, am not buying Spore because of its DRM.

Here are two polls about DRM:

The ZDNet poll is obviously very biased, since the article in which it appears is strongly critical of DRM. On the other hand, I think the GameFAQs poll paints a very good picture of the situation: most PC gamers have been somewhat inconvenienced by DRM or outright hate it. Where do you stand?


Happy 10th birthday, Google!

September 7, 2008

On September 7, 1998, Google was first incorporated as a privately held company. Happy 10th birthday! What a decade it has been for Google. If you haven’t read it already, I highly recommend reading The Anatomy of a Search Engine, the original paper published by Larry Page and Sergey Brin in 1998 while they were developing the first version of Google at Stanford.

Some other papers about other Google technologies which I also recommend reading:

  • MapReduce: a framework for scaling data processing to thousands of computers, based on the functional programming primitives map and reduce.
  • BigTable: a database which scales to petabytes of data. Conventional relational databases scale to reasonably large sizes, but not the enormous amount of data that Google needs to work with.

(What, you thought this post was going to be about Chrome?)

Comments Off on Happy 10th birthday, Google!

Pizza Party

September 6, 2008

Domino\'s Pizza
Domino’s Pizza allows you to order pizza over the Internet. For Unix geeks like Cory Arcangel, the natural extension to that is ordering pizza from the command line. Why bother bother dealing with a web browser and all those messy flash applications when you can just type

pizza_party -pmx 2 medium regular

to get two medium regular-crust pizzas with (p)epperoni, (m)ushroom, and e(x)tra cheese? pizza_party is a Perl script which parses the various options and login info and generates a few HTTP requests to the right places, complete with a man page. I’m particularly amused by the line from the README:

if you don’t know how to install Perl modules, you’re clearly not hard enough to be ordering pizza at the command line.

I haven’t tried it out yet, mostly because I don’t have a Domino’s account and I’m not a huge fan of theirs, but I’ll give it a shot next time I’m in the mood. pizza_party requires your username and password, either on the command line or from the .pizza_partyrc file (stored in plaintext), so if you use it, don’t use a password you use for anything else. No credit card information is involved at all, you pay on delivery as usual.


Comments Off on Pizza Party