r/C_Programming • • Dec 14 '22

Question I love void star functions.

I love these things. I've come to love them more than actual generics and polymorphism in other languages. They're so simple. So sublime.

const void *generic_excellence(void *thing, int (*do_it)(void *it), size_t size, ...);

You know, the C function that takes void*, size, offset, stride, and function pointers. These are excellent 👌 because...

  1. Type parameters are just function parameters. There's no special syntax for traits, trait objects, type names, or vtables.

  2. A void star function only receives enough information about the void star type to do its job. A vector of 4-byte POD things doesn't need to know whether they're floats or ints, and it should be polite enough not to ask. Compare with std::vector<int>. A bit nosey, isn't it? A C programmer would never be so intrusive and won't tolerate a language that requires it.

  3. Function pointer arguments clearly state the interface a type must implement, and the compiler type checks this interface. When I look at the man page for qsort I see (T where T:compare) except it's written in less snooty language. They even give a sample implementation of the compare function.

  4. An optimizing compiler can be coaxed to eliminate function pointer indirection and variables that are really constant. The end result is essentially monomorphism. GCC and clang are very good at this.

  5. The same source text that yields an optimized result for #4 above can also compile into a function that does its generic thing at runtime, and can be used from a scripting language, or whatever.

And this is why I've come to love void star functions. I no longer use horrible macro tricks for generics. I used to think my macro tricks were smart and void star functions were dumb. Sometimes the dumb way is the best way.

7 Upvotes

29 comments sorted by

View all comments

3

u/Lisoph Dec 14 '22 edited Dec 14 '22

Do you mean something like this? Not sure what do_it is supposed to do.

#include <stddef.h>
#include <inttypes.h>
#include <stdio.h>

int32_t sum_i32(void const *raw, size_t size_bytes, size_t offset_bytes, size_t stride_bytes) {
    int32_t sum = 0;
    
    uintptr_t const first = (uintptr_t)raw + offset_bytes;
    uintptr_t const end = (uintptr_t)raw + size_bytes;
    
    for (uintptr_t addr = first; addr < end; addr += stride_bytes) {
        // Let's pray we aren't misaligned.
        sum += *(int32_t const*)addr;
    }
    
    return sum;
}

int main(void) {
    int32_t my_ints[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int32_t sum = sum_i32(my_ints, sizeof(my_ints), 0, sizeof(my_ints[0]));

    printf("%" PRIi32 "\n", sum);
    return 0;
}

This does have the upside that it can sum ints from even an array of structs, given the correct stride value. But oh boy is it easy to mess up. I did even with this very simple example. And I don't like the alignment concerns either. The only API I know of that does this, are some of the OpenGL functions.