Emacs functions for toggling between “.” and “->” in C code

The first function simply toggles the characters at the point between “.” and “->”. Not terribly useful.

The second function is a little more interesting. You can put the point by a struct-type variable which is or isn’t a pointer and this function will toggle both the “*” and all of the “.”s and “->”s in the function.

Continue reading

An unexpected peril of mixing C and C++

The other day at work we ran into a nasty little bug that was quite tricky and chewed up quite a few hours of a few people’s time. So I thought that as a service to you, my loyal reader, I would post about it and warn you of this tricky little issue that deals with how gcc (or at least some versions of it, including the ancient version that we happen to be using) treats empty structs differently, depending on whether you’re compiling as C or C++…

Continue reading

I hate #define macros

I’m working with some C code that is quite well-written but makes a lot of use of some fairly complicated #define macros. Macros are discouraged by the coding guidelines, but they are a necessary evil here. They could’ve been functions except for the fact that they allocate memory on the stack using alloca and a function would destroy the memory when it exited. Writing these functions as #define macros keeps everything in the caller’s stack frame so the allocated memory remains valid.

The only problem is that #define macros are a pain in the butt to write and debug. You have to backslash every line (which looks like a mess) and if you forget you get weird errors. You also have to be careful about putting things in scopes and/or giving them funky names to avoid namespace issues. When you do something wrong, you get really weird errors that point you at the calling code instead of the macro itself (because #define macros essentially are a string replacement).

Too bad we’re not using C++. C++ smart pointers would obviate the need for alloca, which in turn would obviate the need for the macros.