{"id":4919,"date":"2021-08-14T00:01:59","date_gmt":"2021-08-14T07:01:59","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4919"},"modified":"2021-08-07T08:39:58","modified_gmt":"2021-08-07T15:39:58","slug":"negative-array-elements","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4919","title":{"rendered":"Negative Array Elements"},"content":{"rendered":"<p>I enjoy it when an experienced coder reviews my stuff. I&#8217;m happy with the feedback, and occasionally they toss me a bone. This time it was a rather obscure bone, but I see the point: Avoid using negative array elements.<br \/>\n<!--more--><br \/>\nSomewhere in a code example, I use a variable to set an array&#8217;s dimension. This construction is rare, but it&#8217;s possible. Specifically, I was admonished for using an <em>unsigned<\/em> variable to initialize the array. So I got thinking . . .<\/p>\n<p>Consider the source code below, where <em>signed int<\/em> variable <code>a<\/code> is initialized to the value -10. This variable then sets the number of elements in <em>int<\/em> array <code>values[]<\/code>. The program outputs the amount of memory allocated for the array as well as the number of elements.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_08_14-Lesson.c\" rel=\"noopener\" target=\"_blank\">2021_08_14-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    signed a = -10;\r\n    int values[a];\r\n\r\n    printf(\"Array 'values' occupies %zu bytes of storage\\n\",sizeof(values));\r\n    printf(\"It contains %ld elements\\n\",sizeof(values)\/sizeof(int));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Array 'values' occupies 17179869144 bytes of storage<br \/>\nIt contains 4294967286 elements<\/code><\/p>\n<p>Let me translate: When initialized to -10 elements, the array consumes 17 gigabytes of storage. It contains over 4 billion elements.<\/p>\n<p>My computer has 32GB of RAM, so these are legitimate values. Further, a 12 line C program run at the command prompt grabbed over half the RAM in the system. Impressive.<\/p>\n<p>Obviously, what&#8217;s going on is that the compiler is reading the value -10 as <em>unsigned<\/em>, not <em>signed<\/em>. For a 32-bit integer value, the <em>unsigned<\/em> equivalent of -10 is 17179869144 or 0x3FFFFFFD8. I think. I&#8217;m not even sure. All I know, is that it&#8217;s a Bad Thing to do.<\/p>\n<p>This approach is the only way you can assign negative array elements. If you try to be direct:<\/p>\n<p><code>int values[-10];<\/code><\/p>\n<p>The compiler pukes up an error message; can&#8217;t be done!<\/p>\n<p>Still, you <em>can<\/em> use a variable set array size. If you do so, ensure that you use an <em>unsigned<\/em> data type. Or, do what I do in these cases: Allocate a pointer instead.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Perhaps not in this universe, but are negative array elements even a thing? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4919\">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-4919","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\/4919","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=4919"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4919\/revisions"}],"predecessor-version":[{"id":4930,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4919\/revisions\/4930"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}