<story><title>The Linux Kernel Is Now VLA (Variable-Length Array) Free</title><url>https://www.phoronix.com/scan.php?page=news_item&amp;px=Linux-Kills-The-VLA</url></story><parent_chain><item><author>enriquto</author><text>Yeah, VLA is one of my favorite features in C.  I would love if it was viable to use them for arbitrarily large temporary arrays.  They lead to much cleaner code.  Instead of&lt;p&gt;&lt;pre&gt;&lt;code&gt;    {
            float *x = malloc(n * sizeof*x);
            ...
            free(x);
    }
&lt;/code&gt;&lt;/pre&gt;
you do simply&lt;p&gt;&lt;pre&gt;&lt;code&gt;    {
            float x[n];
            ...
    }

&lt;/code&gt;&lt;/pre&gt;
In image processing, you often need large temporary images, but it is dangerous to distribute code such as the above unless you play with the stack limits from outside your program.</text></item><item><author>int_19h</author><text>I&amp;#x27;m actually kinda sad that VLAs didn&amp;#x27;t work out in general (they have been downgraded to an optional feature in the latest C standards). For all the downsides, they make working with matrices in C much easier.</text></item></parent_chain><comment><author>rwmj</author><text>It&amp;#x27;s non-standard (although we have tried to get it added to the next Cxx standard) but you can use __attribute__((cleanup)) here.  In systemd code it is common to write this as:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    _cleanup_free_ float *x = malloc (...);
&lt;/code&gt;&lt;/pre&gt;
Since cleanups are supported by both GCC and Clang it&amp;#x27;s not a real problem to use them on Linux, BSD and MacOS.  We recently added them to libvirt, have used them in libguestfs for a long time, and as I mention above they are used in systemd for years.  They are also applicable to many other cases such as automatically closing files at the end of the current scope.</text></comment>
<story><title>The Linux Kernel Is Now VLA (Variable-Length Array) Free</title><url>https://www.phoronix.com/scan.php?page=news_item&amp;px=Linux-Kills-The-VLA</url></story><parent_chain><item><author>enriquto</author><text>Yeah, VLA is one of my favorite features in C.  I would love if it was viable to use them for arbitrarily large temporary arrays.  They lead to much cleaner code.  Instead of&lt;p&gt;&lt;pre&gt;&lt;code&gt;    {
            float *x = malloc(n * sizeof*x);
            ...
            free(x);
    }
&lt;/code&gt;&lt;/pre&gt;
you do simply&lt;p&gt;&lt;pre&gt;&lt;code&gt;    {
            float x[n];
            ...
    }

&lt;/code&gt;&lt;/pre&gt;
In image processing, you often need large temporary images, but it is dangerous to distribute code such as the above unless you play with the stack limits from outside your program.</text></item><item><author>int_19h</author><text>I&amp;#x27;m actually kinda sad that VLAs didn&amp;#x27;t work out in general (they have been downgraded to an optional feature in the latest C standards). For all the downsides, they make working with matrices in C much easier.</text></item></parent_chain><comment><author>dzdt</author><text>Its unfortunate that C and C++ kept the mindset that automatically memory managed variables are stored on a SMALL stack, with the consequence of exceeding that small and unknown size being that your program crashes.&lt;p&gt;For variable sized arrays, there is already a bit of overhead in sizing the allocation.  Would it really have been impossible to move large allocations to the heap with automatic free at exit of the scope?</text></comment>