Close

zero-cost abstractions for-in loops in C

marblemarble wrote 03/09/2021 at 22:59 • 2 min read • Like

This is a simple example of zero cost abstractions that might make your code easier to read.

I really like the structure of the for loop in python. It almost sounds like a normal English sentence and is therefore more intuitive to understand.

for item in iterable:
    do_stuff_with(item)

I wanted a similar syntax in C. This can easily be done with two macros.

The COUNT_OF macro is from the Chromium code base and computes the number of elements in a C array.

The FOR_IN macro takes a data type, variable name, array reference and code to execute with the variable. It constructs a normal C for loop with these inputs.

for(type *var = list; var < (type *)(list+COUNT_OF(list)); var++){
    //code
}

 In the following code you can see both macros and some code using them.

#include <stdio.h>

#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))

#define FOR_IN(VAR, LIST, \
            CODE)\
        for(typeof(LIST[0]) *VAR = LIST; VAR < (typeof(LIST[0]) *)(LIST + COUNT_OF(LIST)); VAR++)\
            CODE

typedef struct {
    char c;
} foo_s_t;

unsigned int ints[] = {1, 2, 3};

void main(void) {
    FOR_IN(i_p, ints, {
        printf("%d\n", *i_p);
    })

    foo_s_t foos[] = {
        {.c = 'H'},
        {.c = 'i'}
    };

    FOR_IN(foo_s_p, foos, {
        printf("%c", foo_s_p->c);
    })
}

 This is the resulting output.

$ ./a.out
1
2
3
Hi 
Like

Discussions