{"id":3488,"date":"2019-02-09T00:01:24","date_gmt":"2019-02-09T08:01:24","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3488"},"modified":"2019-02-02T10:29:49","modified_gmt":"2019-02-02T18:29:49","slug":"variable-length-arrays","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3488","title":{"rendered":"Variable Length Arrays"},"content":{"rendered":"<p>The C99 standard added a feature to dimension an array on-the-fly. The official term is <em>variable length array<\/em> or VLA. And while you might think everyone would love it, they don&#8217;t.<br \/>\n<!--more--><br \/>\nMy observation is that instead of dynamically allocating an array, most coders really want to <em>reallocate<\/em> an array as a program runs. For example, you create the array <code>name[32]<\/code> and while the code runs you need four more bytes, so you can issue a &#8220;realloc&#8221; command to make the array <code>name[36]<\/code>. That&#8217;s a feature I think some coders might appreciate, but an on-the-fly array allocation?<\/p>\n<p>One way to implement a VLA is to specify the array size as a variable, as is done in the following code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid array(int size)\r\n{\r\n    char alpha[size];\r\n    int x = 0;\r\n\r\n    while(x &lt; size)\r\n    {\r\n        alpha[x] = 'A' + x;\r\n        putchar(alpha[x]);\r\n        x++;\r\n        if( x &gt; 26 )\r\n            break;\r\n    }\r\n    putchar('\\n');\r\n}\r\n\r\nint main()\r\n{\r\n    array(5);\r\n    array(20);\r\n    array(9);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>array()<\/em> function sets the size of the <code>alpha[]<\/code> array based on the <code>size<\/code> argument. Depending on the value passed, the array is stuffed to a varying degree with upper case letters of the alphabet. Here&#8217;s the output:<\/p>\n<p><code>ABCDE<br \/>\nABCDEFGHIJKLMNOPQRST<br \/>\nABCDEFGHI<\/code><\/p>\n<p>In each call, the size of the <code>alpha[]<\/code> array is different. I didn&#8217;t try to implement a similar function that doesn&#8217;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.<\/p>\n<p>From the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Variable-length_array\" rel=\"noopener\" target=\"_blank\">Wikipedia page<\/a> on variable length arrays, Linus Torvalds is quoted saying:<\/p>\n<blockquote><p>&#8220;USING VLA&#8217;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.&#8221;<\/p><\/blockquote>\n<p>Torvalds goes on to say that Linux is free of VLAs and he&#8217;s proud of that fact. I&#8217;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&#8217; observation.<\/p>\n<p>It&#8217;s possible in C to create a variable length array. It&#8217;s just not necessary and it&#8217;s something I would recommend you avoid: If you need to dynamically allocate and even reallocate storage buffers, you can always use pointers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s possible to size an array while your code is running, but few programmers try. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3488\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3488","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3488","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3488"}],"version-history":[{"count":11,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3488\/revisions"}],"predecessor-version":[{"id":3504,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3488\/revisions\/3504"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}