Expanding Macros into String Constants in C

Today I came across an annoying problem: how do I expand a C macro into a string?

One of C’s preprocessor operators is #, which surrounds the token that follows it in the replacement text with double quotes (“). So, at first the solution sounds pretty simple: just define

#define STR(tok) #tok

and things will work. However, there is one caveat: it will not work if it is passed another macro. For example,

#define BUF_LEN 100
#define STR(tok) #tok

STR(BUF_LEN)

will produce, after going through the preprocessor,

"BUF_LEN"

instead of "100", which is undesired. This behavior is due to the C standard noting that no macro expansions should happen to a token preceded by #.

However, after reconsidering the source of the problem, I’ve found the following workaround: define another macro that will expand the argument and only then call the macro that does the quoting.

#define STR_EXPAND(tok) #tok
#define STR(tok) STR_EXPAND(tok)

#define BUF_LEN 100

STR(BUF_LEN)

will produce

"100"

as desired.

Explanation: The STR macro calls the STR_EXPAND macro with its argument. Unlike in the first example, this time the parameter is checked for macro expansions and evaluated by the preprocessor before being passed to STR_EXPAND, which quotes it, thus giving the desired behavior.

spass 1.1 – Secure Password Generator

This is a new version of my /dev/random-based secure password generator – spass. The new version doesn’t have any new features; it’s mainly a bug-fix release. The package now uses autotools, which means it has the standard configure script and makefile. I also fixed some typos in the help message. Overall, the new version doesn’t offer anything new compared to the old one, except for easier installation.
Continue reading spass 1.1 – Secure Password Generator

mctext 0.2 – A Markov Chain Text Generator

This is the second release of my Markov Chain text generator – mctext. This text generator takes existing sample text and generates new text using Markov Chains.

The main new thing in this version is that it allows users to specify via the command line how many words should be considered when generating the next one. The bigger the step number, the closer the generated text is to the original one. The value used in mctext-0.1 was 2, and this is also the default in this one. The number of steps can be set using the --steps command line switch.
Continue reading mctext 0.2 – A Markov Chain Text Generator

Setting Up OmniComplete (Autocompletion) for wxWidgets in Vim

I use Vim as my main IDE for C/C++-related development (as well as for almost all other development). If you use (or are thinking about using) Vim as an IDE, you better get some good autocompletion functionality. This kind of autocompletion is provided by OmniComplete, which has been available since Vim 7.0. Just having OmniComplete is a nice thing, but it’s much more helpful if it’s configured properly to work with the libraries you use, such as wxWidgets. In this post, I will show you how to get OmniComplete working for wxWidgets. However, the procedure I will show can be easily adapted to almost all libraries.
Continue reading Setting Up OmniComplete (Autocompletion) for wxWidgets in Vim

C’s “Goes To” Operator

Well, it isn’t really an operator, but this is a nice C code construct I ran into. It doesn’t seem to have any benefit except as a nice way to create a reverse loop:

int count = 100;
while (count-->0) {
        //some work
}

As I said, I don’t think I’ll find any performance benefit in using this code snippet. On the other hand, it is always fun to see the puzzled face other programmers have the first time they see the code.

Optimizing for Loops: Reverse Loops

for loops are basic language constructs in many languages. One of the first things to look at when optimizing code is loops, as they do considerable amounts of work (like going through a very large amount of data) in very little code.

If you use a for loop, but you don’t really care about the order in which the loop is executed – to be more precise, if you can afford reversing the loop – you can save quite some time. By reversing the loop, I mean that instead of giving the index values from 0 to 10, for example, you go from 10 downward to zero. This doesn’t seem like a big change, but when carefully implemented, this can easily improve the performance of your for loops.
Continue reading Optimizing for Loops: Reverse Loops

The Revised String Iteration Benchmark

In this post I’m going to discuss again the string benchmark I did before to find out what is the fastest way to iterate over an std::string. If you haven’t read the previous post on this subject, go ahead and read it, as it covers the basic idea behind this benchmark. As I did the last time I ran the benchmark, I checked 5 ways of iteration:
Continue reading The Revised String Iteration Benchmark

Profiling Code Using clock_gettime

After raising the issue of the low-resolution problem of the timer provided by clock() in Resolution Problems in clock(), I ended the post by mentioning two more functions that should provide high-resolution timers suitable for profiling code. In this post, I will discuss one of them, clock_gettime().
Continue reading Profiling Code Using clock_gettime

Random – A Random Number Generator Class

After dealing with the seeding of srand(), I’ve realized that rand() just doesn’t generate random numbers that are strong enough for some of my needs (e.g. a strong password generator), so I decided to find a better solution. The solution came in the form of Random, a cryptographically strong pseudo-random number generator class.
Continue reading Random – A Random Number Generator Class

Resolution Problems in clock()

While recently playing with clock() in order to time the performance of different kinds of code and algorithms, I found an annoying bug. clock() just can’t register work that has taken less than 0.01 seconds. This is pretty unexpected, as clock() should return the processor time used by the program. The man page for clock() states:

The clock() function returns an approximation of processor time used by the program.

Continue reading Resolution Problems in clock()