-2

How does it internally work in case of compiler extensions? Will this still be called static compile time memory allocation?

int size;
cin>>size;
int arr[size]; 
  • How can something be done at compile time that relies on information not provided until runtime? – Scott Hunter Jan 07 '22 at 19:43
  • 2
    Check this link https://stackoverflow.com/questions/8271773/is-it-possible-to-allow-a-user-to-enter-an-array-size-with-a-keyboard/8272564 – XraySensei Jan 07 '22 at 19:44
  • In standard c++ array size must be a compile-time constant. So this code should be reject by the standard-conforming compiler simply because c++ standard says that it is invalid. – user7860670 Jan 07 '22 at 19:45
  • 1
    It is considered wrong because the language's standard says that it is not allowed. Some compilers allow it as an extension, but in those cases it is bad practice because it breaks portability, the code won't work on other conforming compilers. – François Andrieux Jan 07 '22 at 19:46
  • *//Why is this considered wrong?* -- Use Visual C++, and you will quickly see why it's wrong. The compiler completely rejects that syntax. – PaulMcKenzie Jan 07 '22 at 19:48
  • Ok agreed that a standard-conforming compiler will reject this code and that makes sense too but I want to understand what happens in compiler extensions that do accept this? Why and how does it work ? – My thoughts Jan 07 '22 at 19:53
  • Because the C++ standard says so : https://eel.is/c++draft/dcl.array. D1[*constant expression*]. Thus the size of an array cannot be a variable. And since the https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines recommend not to use new/delete the best way to use dynamically (re)sizeable arrays is to use std::vector. – Pepijn Kramer Jan 07 '22 at 19:58
  • Here's what [GCC has to say about its VLA implementation](https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html). Since they've copied freely from the C99 Standard for their implementation [cppreference's page on C arrays](https://en.cppreference.com/w/c/language/array#Variable-length_arrays) and the relevant portions of the C Standard (Don't have a linkable copy, sorry) may be helpful to understand what the rules are. – user4581301 Jan 07 '22 at 20:20
  • In general, avoid the things like the plague or you'll probably wind up like [this poor sucker](https://stackoverflow.com/questions/70624690). – user4581301 Jan 07 '22 at 20:24
  • A conforming C++ implementation is not required to **reject** this code. It is required to **issue a diagnostic**, where a "diagnostic" is one of a set of implementation-defined messages. Having done that, a conforming compiler can continue compiling the code, giving it an implementation-specific meaning. – Pete Becker Jan 07 '22 at 20:50
  • In **C** (since it was originally a C feature, that some compilers allows in C++ as an extension), the facility is considered a misfeature because of the inability to assess if it will blow out the stack at compile time. – Eljay Jan 07 '22 at 20:58
  • @Mythoughts Also on a side note, implementations of VLA's in some previous g++ compiler versions are broken if they are used as arguments (notably as iterators) to various STL algorithm functions. I can't pinpoint the exact post where I saw this, but I ran into this some time ago, along with a few other SO volunteers. Now, did g++ need to fix this? Maybe, but they would have been their right to not fix it, as VLA's were not part of standard C++. They would have claimed "use at your own risk". – PaulMcKenzie Jan 07 '22 at 22:34

1 Answers1

0

It's wrong because user input happens at runtime which is after compilation. Array variable size must be compile time constant in c++, which contradicts with the premise of runtime input.

eerorika
  • 232,697
  • 12
  • 197
  • 326