{"id":3853,"date":"2014-03-30T06:02:03","date_gmt":"2014-03-30T06:02:03","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-am-i-able-to-declare-an-array-with-variable-length-determined-at-runtime-in-c-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:02:03","modified_gmt":"2014-03-30T06:02:03","slug":"how-am-i-able-to-declare-an-array-with-variable-length-determined-at-runtime-in-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-am-i-able-to-declare-an-array-with-variable-length-determined-at-runtime-in-c-collection-of-common-programming-errors\/","title":{"rendered":"how am i able to declare an array with variable length determined at runtime in C++?-Collection of common programming errors"},"content":{"rendered":"<p>Please check this code out it compiles and runs absolutely fine.. The question is that when i started learning c++ (turbo c++) i never was able to declare an array of any type as ..<\/p>\n<pre><code>datatype var[variable_set_at_runtime];\n<\/code><\/pre>\n<p>and i took it for granted that this cant be possible in latest gcc compilers&#8230;but surprisingly this is possible&#8230;<\/p>\n<p>So my related question is that whats the need of new operator then??<\/p>\n<p>I know that new operator does a lot of things including dynamically allocating memory at runtime in heap,returning an address to that resource&#8230;etc..<\/p>\n<p>What difference i feel is that my code dynamically allocates the memory on stack while new does it on heap?? is this the only difference&#8230;<\/p>\n<p>i am really astonished after writing something like this as i could not do this when i started learning c++ and &#8230;to add to it i can do this for custom data types too&#8230; :-O<\/p>\n<pre><code>#include\nusing namespace std;\nstruct A\n{\n    int a;\n    int b;\n};\nint main()\n{\n    int a;\n    cin&gt;&gt;a;\n    int ch[a];\n    for(int i=0;i<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Please check this code out it compiles and runs absolutely fine.. The question is that when i started learning c++ (turbo c++) i never was able to declare an array of any type as .. datatype var[variable_set_at_runtime]; and i took it for granted that this cant be possible in latest gcc compilers&#8230;but surprisingly this is [&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-3853","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3853","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=3853"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3853\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}