Skip to content

Comment on Microsoft, please support (at least a tiny bit of) C99parent

Comments

I don't dispute for a moment that some people wanted VLAs or that they're useful in some contexts. On the other hand, your cited field is relatively small, and the use case could have been satisfied with a mechanism much more in line with the existing language.

In fact, we've de-facto had such a mechanism for decades, but it hasn't been codified anywhere, actually for some of the same reasons people don't appreciate VLAs, but it at least rests much more comfortably with the rest of the language.

At some point, you go from enhancing C to breaking it in the interests of a small minority (who apparently don't want to use Fortran?). VLAs are, if not over the line, right on it.

On the other hand [...] the use case could have been satisfied with a mechanism much more in line with the existing language.

In fact, we've de-facto had such a mechanism for decades

Unfortunately, that's not the case. There are actually two primary use cases for VLAs:

1. Variable-length multi-dimensional array parameters:

    // C99
    double trace(size_t n, double mat[][n])
    {
        double sum = 0;
        for(size_t i = 0; i < n; ++i)
            sum += mat[i][i];
        return sum;
    }


    /* C90 */
    double trace(size_t n, double mat[])
    {
        double sum = 0;
        size_t i = 0;
        for(; i < n; ++i)
            sum += mat[i * n + i];
        return sum;
    }
While this may not look like much of an improvement in simple cases, not having to manually emulate array subscription is quite convenient in more complex ones.

2. Allocation of variably-sized objects with automatic storage duration. Some libc implementations provide alloca() for that purpose -- unfortunately, it has issues (see eg http://c-faq.com/malloc/alloca.glb.html ).

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.