Monday, January 25, 2010

Wading into the C

No time for deep thoughts these days; too much hacking, dissertating, designing, and committeefying going on. Just a couple notes based on recent experiences hacking in C/C++:

1. Not being able to rely on recursion makes me sad.

2. "Downwards macro-args" in C:
#define MY_ENUM_LIST(m) \
m(RED, 0), \
m(GREEN, 1), \
m(BLUE, 2)

#define DEF_ENUM_ENTRY(c, v) c = v
#define QUOTE_ENUM_ENTRY(c, v) #c

typedef enum rgb {
MY_ENUM_LIST(DEF_ENUM_ENTRY)
} rgb;

const char *rgb_names[] = {
MY_ENUM_LIST(QUOTE_ENUM_ENTRY)
};
3. I am fast becoming acquainted with gdb.

4. And a few choice command-line shortcuts really save extraordinary amounts of time. My new fav: the !? bash-history shortcut.

7 comments:

Nels said...

Whoa!

Okay, re: #2. I get the rgb typedef... But what does is mean to say #RED or #GREEN in C?

Dave Herman said...

It's a CPP-ism, not a C-ism-- when you prefix a macro argument with # it expands into the string literal representing the token source.

steck said...

Are you hep to bash command completion? Just learned about that neato feature recently.

shapr said...

I use C-r to search backwards in my command history, is that the same thing?

Admittedly, I use zsh

Ronald Garcia said...

If you're going to go the preprocessor route, you should take a peek at the Boost Preprocessor Library. Amazing/scary what you can do with it.
IIRC, C99's preprocessor is expressive enough to write a small lisp interpreter.

Leo Meyerovich said...

Now I remember why I'm writing my own browser and not dealing with yours ;-)

Felix said...

gdb tip: make command scripts.

e.g. I make a file, 'gdbcmds' that look like:

b 'avmplus::MathClass::pow'
r -Dinterp ../../interp64_busted.abc
finish
finish
finish
step
step
next
next

And then I run my binary like so:

% gdb avmshell -x gdbcmds

Saves a lot of time reestablishing a context where I found some behavior I want to investigate, which can involve a lot of retyping (and more importantly, rethinking and missteps!).

Of course a script like the one above won't stay useful for very long, but its just meant to be a throwaway during a single day.