Skip to content

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

Comments

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.