Saturday, November 24, 2012

Randolph the Red-Head Sea Lion

This one goes out to all the beach bums and surfers out there...

You know Swimmer and Diver and Shelly and Cowling...
Comet and Pooper and Beachbum and Barker...
But do you recall,
the most famous sea lion of all..
Randolph the red-head sea-lion,
had a very itchy ear.
And if you ever saw it,
you would even say "oh dear!"
All of the other sea lions,
used to laugh and call him names.
When he was tagged by a marine-biologist,
they gave him all the blame.
Then one foggy Christmas Eve,
Santa came to say:
"Randolph with your GPS tracker so bright,
won't you pull my board tonight."
Then all the sea lions loved him;
as they shouted out in horror,
"Oh, for the love of christ!
Santa's wearing that damn speedo again!"

Apologies to Johnny Marks, wherever you are...


Sunday, August 5, 2012

Call your senators; tell them to support S. 3211

I'm writing this a few hours before we find out whether Mars Science Laboratory / Curiosity survives the plunge into the martian atmosphere. If you're like me, every time we send a craft into space, you get a lump in your throat and a tear in your eye. We're using science, technology and the adventurer's spirit to extend ourselves into the sky. How audacious is that!?

This summer, the United States Senate will vote on a measure that will facilitate future missions into orbit (and beyond.) And the best thing about it is it won't cost you a dime. It's called Senate bill 3211, "Safeguarding United States Satellite Leadership and Security Act of 2012" and has been referred to the Foreign Relations Committee for review. We need to tell our Senators (especially the ones on the Foreign Relations Committee) that we, the public, support this measure or else it risks "dying in committee."

Like a similar amendment in the House of Representatives, this bill allows employees of the fledgling private space industry talk to and make partnerships with foreign companies. It allows the President to loosen cold-war era regulations designed to keep U.S. missile secrets from the Soviet Union. As you're aware, the Soviet Union dissolved decades ago and we are now partners with the successor state, the Russian Federation, in the International Space Station.

The rest of the world is already starting to surpass the United States in small private space businesses. US businesses are at a distinct disadvantage because we cannot effectively participate in international consortia or partnerships to develop space technology. While we can put men on the moon and rovers on mars, the next phase of space technology development will be by small and medium sized firms, working outside the government umbrella.

It may sound like a dangerous thing to do, letting foreigners learn our valuable secrets. But it turns out, we're not the only ones with space technology. Europe, India, China, Japan all have successful programs. When it's time for the developing world to launch their next (or first) space mission, will they turn to us, only to be buried in paperwork and barred from discussing key technical details? Or will they turn to competing firms in foreign countries?

The US Department of Defense, who hopes to grow their use of Commercial Off-The-Shelf (COTS) technology, recently released the "1248 Report" dealing with policy implications of easing export restrictions on space technology. The report concludes that maintaining the status quo has "limited national security benefits" and liberalizing exports would "enhance the competitiveness of the U.S. space industrial base, while continuing to protect U.S. national security needs."

So please... Call Your Senators. Tell them you support Senate 3211. If you live in California, Delaware, Florida, Georgia, Idaho, Illinois, Indiana, Maryland, Massachusetts, New Hampshire, New Jersey, New Mexico, Pennsylvania, South Carolina, Tennessee, Oklahoma, Utah, Virginia or Wyoming, one of your senators is a member of the Foreign Relations Committee which is now considering this bill. Call Them. Tell them you want S. 3211 to get the thumbs up from the Foreign Relations Committee and the whole Senate!

You can find the phone number and email address of your senators from the senate.gov web site (look in the upper right corner for the "Find Your Senators" drop-down.) Call Them. If you don't know what to say, this is the message I left for Barbara Boxer and Dianne Feinstein; use it as a template:

"Thank you for taking my call. My name is <your name here> and I am a resident of <your state here>. I am calling to register my support for Senate 3211, the 'Safeguarding United States Satellite Leadership and Security Act of 2012.' It is currently being considered in the Foreign Relations Committee and I ask your help in ensuring it successfully exits the committee and is passed by a full vote of the senate. I believe this bill is important for the growing private-sector space industry. Without it, we may lose our leadership in space technology. Thank you again for your time."

Thank you in advance for taking the time to read this blog post and ping me via Twitter at @OhMeadhbh after you've called your Senators!

Saturday, May 12, 2012

strange programmer habits : do...while(0) in macros

I wrote previously about the strange habit some developers have of using do...while(0) to avoid using gotos. After a recent conversation with my co-worker Cory, I learned do...while(0) blocks are even more useful.

Consider the situation where you want to have a C macro that swaps two values. Here's a naive implementation that swaps two integers:
#define swap( a, b ) int temp = a; a = b; b = temp;
And you might use it like this:
int x = 12;
int y = 17;
swap( x, y );
printf( "(x,y) = (%d, %d)\n", x, y );
And since you executed the swap after assigning values to x & y and before printing, you would expect this fragment to print out something like this:
(x,y) = (17,12)
But there are problems with this macro. Let's say we want to use it in an if...else clause, like this:
int accumulator = 2317;
int count = 0;
/* do some stuff */
if( accumulator > 1000 )
  swap( x, y );
else
  count++; 
If you tried something like this, you would probably get an error telling you the compiler found an else unrelated to a previous if. And here's why...

C macros operate like direct textual replacements, so when the compiler sees the string "swap( x, y )" it replaces it with whatever value it had for the macro, producing an if...else clause like this:
if( accumulator > 1000 )
  int temp = x;
  x = y;
  y = temp;;
else
  count++;
The syntax of the C language says that you get either a curly-brace enclosed block or a single statement following an if (). The problem here is that macro substitution has inserted multiple statements (that are not part of a curly-brace enclosed block) after the if. You could define the swap macro like this:
#define swap( a, b ) { int temp = a; a = b; b = temp; }
But this won't work outside an if statement.
And this is where the do { ... } while( 0 ) comes in handy. If we define our swap macro like this, we'll get the correct behavior in if's, for's and as just plain statements:
#define swap( a, b ) do { int temp = a; a = b; b = temp; } while ( 0 )
Try it yourself; here's a quick program that defines two macros: swap and swap_naive. The former "does the right thing" while the latter doesn't. The program accepts a command line option, converts it to an integer and swaps different variable depending on whether the option is greater than 50. This program won't compile until you replace the "swap_naive" macro calls with plain ol' "swap" calls:

#include <stdio.h>
#include 
<stdlib.h>
#define swap_naive( a, b ) temp = a; a = b; b = temp;
#define swap( a, b ) do { int t = a; a = b; b = t; } while ( 0 )
void main( int argc, char *argv[] ) {
  int x = 17;
  int y = 19;
  int z = 23;
  int temp;
  int c;
  if( argc > 1 ) {
    c = atoi( argv[1] );
    printf( "comparison value lifted from command line: %d\n", c );
  } else {
    c = 12;
    printf( "using default comparison value: 12\n" );
  }
  printf( "x, y and z before swap: x = %d, y = %d, z = %d\n", x, y, z );
  if( c > 50 )
    swap_naive( x, y );
  else
    swap_naive( y, z );
  printf( "after swap: x = %d, y = %d, z = %d\n", x, y, z );
}

Cheers!