0

So, from what I've studied in C++, we cannot have arrays declared non-dynamically i.e. without the use of "new" keyword as it is against the C++ standard. I always thought that if not done this way, it would throw a compilation error or something and so didn't try it out myself (my bad). However, I now notice my peers using the following format of array declaration without any form of errors :

int n;
cin >> n;
int arr[n];

Now they have always been doing it this way as many of them were unfamiliar with dynamic allocation of memory or vectors and they seemingly never had an issue. Ran it on my machine for confirmation and it worked like a charm. So, my question is how does this work and if this works why bother with dynamic memory allocation? Are there some particular scenarios where this might fail badly?

naruto_022
  • 33
  • 1
  • 6
  • 1
    FWIW, it doesn't actually work. Add the `-pedantic-errors` compiler option to your compile command and watch the code now fail to compile. Run time size arrays is not something that is actually a part of C++ – NathanOliver Jun 07 '21 at 16:52
  • 1
    This is a *variable length array*. They are [not supported in standard C++](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) but some compilers permit them as an extension; it seems like yours is one of them. – Nate Eldredge Jun 07 '21 at 16:53
  • It is rather unfortunate the gcc (probably you are using it) is rather lax on default settings and that variable length arrays look like the simple solution (and that some infamous tutorial sites promote VLAs whenever possible). Use `std::vector` for dynamic arrays – 463035818_is_not_an_ai Jun 07 '21 at 16:53
  • Even when supported, they are absolutely not a complete substitute for dynamic memory allocation. One issue is that their lifetime is only until the function returns, so you can't pass such memory back to the caller - the compiler won't stop you from trying, but it's undefined behavior at runtime. Another is that there is typically no error checking and if you allocate more than the available stack space (usually a few megabytes), your program crashes with no possibility to recover. – Nate Eldredge Jun 07 '21 at 16:55
  • If the user inputs a large enough `n` Automatic storage will be exhausted, Undefined Behaviour will be invoked, and technically the world could come to an end. But more likely you'll just get weird program behaviour and crashes. – user4581301 Jun 07 '21 at 16:59
  • @463035818_is_not_a_number yes I was using gcc, I read the answers suggested, cleared up a lot of stuff for me. It's a total bummer that this is so not known information and many youtube channels and tutorial vids actually promote this. Anyways, thanks – naruto_022 Jun 07 '21 at 17:26
  • @naruto_022 The Internet is not curated and, for the most part, anyone can post anything. This makes the Internet a cesspool that's hard to sift through for the gems of wisdom unless you already know enough about what you are researching to discern whether what you are reading is worthwhile or someone well meaning but accidentally demonstrating [the Dunning-Kruger effect](https://en.wikipedia.org/wiki/Dunning%E2%80%93Kruger_effect). When it comes to something complicated like C++, the Internet is a very bad place to start learning. – user4581301 Jun 07 '21 at 19:02
  • @user4581301 still better than learning from professors who themselves have half the knowledge, imo that would make one very dependent on the person teaching, internet would rather provide diversity in content and if looked properly and analyzed correctly, stuff should still make sense – naruto_022 Jun 07 '21 at 19:36
  • It's irritating, but some instructors (maybe a lot of them) don't really know C++. They got their degrees and doctorates in some other language or from Instructors who didn't know C++ and passed on their misunderstandings. Huge numbers of people, myself included for longer than I care to admit, think they know C++ and use it as C with classes and `vector`. Some of instructors do know proper C++, but are forced to teach from a rubric that's been revised and revamped since the 1960s and is better suited to teaching C or Pascal. Things are changing though. – user4581301 Jun 07 '21 at 19:57

0 Answers0