Variable Length Arrays

The C99 standard added a feature to dimension an array on-the-fly. The official term is variable length array or VLA. And while you might think everyone would love it, they don’t.

My observation is that instead of dynamically allocating an array, most coders really want to reallocate an array as a program runs. For example, you create the array name[32] and while the code runs you need four more bytes, so you can issue a “realloc” command to make the array name[36]. That’s a feature I think some coders might appreciate, but an on-the-fly array allocation?

One way to implement a VLA is to specify the array size as a variable, as is done in the following code:

#include <stdio.h>

void array(int size)
{
    char alpha[size];
    int x = 0;

    while(x < size)
    {
        alpha[x] = 'A' + x;
        putchar(alpha[x]);
        x++;
        if( x > 26 )
            break;
    }
    putchar('\n');
}

int main()
{
    array(5);
    array(20);
    array(9);

    return(0);
}

The array() function sets the size of the alpha[] array based on the size argument. Depending on the value passed, the array is stuffed to a varying degree with upper case letters of the alphabet. Here’s the output:

ABCDE
ABCDEFGHIJKLMNOPQRST
ABCDEFGHI

In each call, the size of the alpha[] array is different. I didn’t try to implement a similar function that doesn’t use a variable-sized array, but it would be easy to allocate a chunk of memory in the function and produce the same output. So the VLA is possible, but not absolutely necessary.

From the Wikipedia page on variable length arrays, Linus Torvalds is quoted saying:

“USING VLA’S IS ACTIVELY STUPID! It generates much more code, and much slower code (and more fragile code), than just using a fixed key size would have done.”

Torvalds goes on to say that Linux is free of VLAs and he’s proud of that fact. I’ve seen a dearth of demo code on VLAs and even less material out there in the C training world. Therefore, I would concur with Mr. Torvalds’ observation.

It’s possible in C to create a variable length array. It’s just not necessary and it’s something I would recommend you avoid: If you need to dynamically allocate and even reallocate storage buffers, you can always use pointers.

2 thoughts on “Variable Length Arrays

  1. The comments on stackoverflow are, for the most part, correct:

    A register variable is an integer stored in one of the processor registers – if possible. The result of this operation is that the value is used efficiently as opposed to being stored and fetched from memory. The register keyword insists that the variable be stored in a register for the duration of the function.

    Modern compilers are optimized to the point where they automatically store frequent variables in registers (from what I’ve read). Therefore, the advice given on stackoverflow and elsewhere is to ignore the keyword and let the compiler optimize your code as it sees fit. Misuse of the register keyword could result in a less-efficient program, so just let it be.

    I had planned on writing a Lesson on register, but it’s difficult to test and demonstrate. I assume that back in the PDP-11 days, it was probably a boon to some code, but today it’s a relic, like some other keywords.

    Thanks for asking!

Leave a Reply