Skip to content

char a[n]; // my discovery of Variable Length Array!

15 December 2011

VLA - Socorro - New Mexico - USA

Today, more than 10 years after its introduction, a shocking surprise: I discovered that a once forbidden practice in the ANSI C (C90) programming language had become normal practice: the use of Variable Length Arrays!

In other words, when I started learning and playing with C, several years ago, one could only define a fixed length array as in:

char name[100];

or if one needed a variable length array, the use of a pointer and the dynamic allocation of the memory was the only choice as in:

char *name;
/* some code... */
name = malloc(100*sizeof(char));
/* some other code... */
free(name);

The discovery of the existence of variable length array (or VLA, not to be confused with those in New Mexico which are the very large ones, beautifully captured in the picture above) undermined my certainties, but surely simplified array handling, especially in the case of multidimensional arrays where the allocation could easily become cumbersome. For instance, in the new ANSI C (C99) one can now thoughtlessly write:

void function(n)
{
    char name[n];
    /* do the crazy stuff... */
}

pretty shocking, isn’t it ?! :)

Writing this post, I also learned that actually the current standard ANSI C is not C99 anymore but C11 which has just been approved by ISO recently (8th december 2011)… who knows what it brings!

Overall, the lesson I took home is that even programming language standards change over time. Another tangible effect of time passing… It is hard, but I will get over it!

L

PS: to know more details, here is a nice post: The New C:Why Variable Length Arrays?

PPS: the way I made the discovery, is thanks to a student who innocently and naturally used the VLA in her code without knowing it. Then when she asked me some help for debugging, I promptly and firmly told her that char x[n]; she was trying to use was certainly wrong… turned out, it wasn’t. :-O

PPS: I’m sorry for the misleading picture, but I challenge you finding a beautiful and more appropriate one for this fascinating topic.

About these ads

From → programming

2 Comments
  1. Simone permalink

    Your hook worked! Variable Length Arrays AND an astronomy observatory? :)
    Although I’ve always worked with my beloved Fortran (and forever I will), I’ve read your post with the same excitement you had when you put it into writing.

    Have a nice day!

    Simone

  2. I knew this nice picture would do the job :))
    Thanks for the feedback, and happy new Fortran year !

    L

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: