{"id":3757,"date":"2014-03-30T05:21:55","date_gmt":"2014-03-30T05:21:55","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/runtime-allocation-of-multidimensional-array-collection-of-common-programming-errors\/"},"modified":"2014-03-30T05:21:55","modified_gmt":"2014-03-30T05:21:55","slug":"runtime-allocation-of-multidimensional-array-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/runtime-allocation-of-multidimensional-array-collection-of-common-programming-errors\/","title":{"rendered":"Runtime allocation of multidimensional array-Collection of common programming errors"},"content":{"rendered":"<p>So far I thought that the following syntax was invalid,<\/p>\n<pre><code>int B[ydim][xdim];\n<\/code><\/pre>\n<p>But today I tried and it worked! I ran it many times to make sure it did not work by chance, even valgrind didn&#8217;t report any <strong>segfault<\/strong> or <strong>memory leak<\/strong>!! I am very surprised. Is it a new feature introduced in g++? I always have used 1D arrays to store matrices by indexing them with correct strides as done with A in the program below. But this new method, as with B, is so simple and elegant that I have always wanted. Is it really safe to use? See the sample program.<\/p>\n<p>PS. I am compiling it with g++-4.4.3, if that matters.<\/p>\n<pre><code>#include \n#include \n\nint test(int ydim, int xdim) {\n\/\/ Allocate 1D array\n    int *A = new int[xdim*ydim](); \/\/ with C++ new operator\n    \/\/ int *A = (int *) malloc(xdim*ydim * sizeof(int)); \/\/ or with C style malloc\n    if (A == NULL)\n        return EXIT_FAILURE;\n\n\/\/ Declare a 2D array of variable size\n    int B[ydim][xdim];\n\n\/\/ populate matrices A and B\n    for(int y = 0; y &lt; ydim; y++) {\n        for(int x = 0; x &lt; xdim; x++) {\n            A[y*xdim + x] = y*xdim + x;\n            B[y][x] = y*xdim + x;\n        }\n    }\n\n\/\/ read out matrix A\n    for(int y = 0; y &lt; ydim; y++) {\n        for(int x = 0; x &lt; xdim; x++)\n            std::cout<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So far I thought that the following syntax was invalid, int B[ydim][xdim]; But today I tried and it worked! I ran it many times to make sure it did not work by chance, even valgrind didn&#8217;t report any segfault or memory leak!! I am very surprised. Is it a new feature introduced in g++? I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3757","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3757"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3757\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}