Why is it allowed to declare an automatic array with size depending on user input? [duplicate]
Why is it allowed to declare an automatic array with size depending on user input? [duplicate]
This question already has an answer here:
I'm using MinGW to compile for C++11 and I found out that this doesn't throw an error:
int S;
cin>>S;
char array[S];
While this does ("storage size of 'array' isn't known"):
char array;
To me, the size is also unknown in the first case, as it depends on what the user input is.
As far as I knew, automatic arrays are allocated at compile time in stack memory. So why wouldn't the first example fail?
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
The first was never originally valid in C or C++. In 1999 it was added to C, and some compilers will support it in C++ too. The compiler can add code to adjust the size of the stack when the array definition is reached. In the second case, no size is provided even at runtime.
– BoBTFish
Aug 29 at 12:44
see Does “int size = 10;” yield a constant expression?
– Shafik Yaghmour
Aug 29 at 13:04
Note, this is an automatic array, not a static one
– M.M
Aug 29 at 14:25
@Deduplicator Your edit kindof invalidated the question because it is not as surprising to determine automatic object sizes at run time...
– Peter A. Schneider
Aug 29 at 15:04
3 Answers
3
It's not. C++ doesn't have variable-length arrays, though some compilers allow it as an extension of the language.
I don't have enough reputation to comment so I am using an answer. Please don't be cruel to me :-)
You are apparently not aware of the GNU GCC extension Arrays of Variable Length. Therefore your first code compiles.
The error message is something different. You have to specify the array length.
gcc
has the -pedantic
switch - enabling this switch the compiler will report your first code as invalid:
gcc
-pedantic
warning: ISO C++ forbids variable length array ‘array’
Read also this thread What is the purpose of using -pedantic in GCC/G++ compiler?
Use very carefully compiler extensions because should you port your code to another compiler then you are in big trouble.
Welcome on SO! This to me looks more as an answer than as a comment, since it does provide the OP useful information about what's going on, addressing the question nicely. In such cases, it's better to avoid answering in comments: answers should be answers. Comments should instead be used for non-answers: e.g., to request more information from the OP.
– chi
Aug 29 at 15:04
[This answers the original version of the question which asked about a static array; Deduplicator corrected this misconception, but now the question is missing a part.]
If your assumption that this piece of code defined a static array were correct, you'd be wondering for a good reason indeed: Something that is determined at compile time, like data with static storage duration, can obviously not depend on user input at run time. This truism is independent of any specific language.
The array defined in your code snippet has, by contrast, automatic storage duration, vulgo is created on the stack. A complete minimal working example would have made the case clearer: It would have shown that the code is in a function.
Objects with automatic storage duration can be created as needed at run time; there is no logical problem preventing that, which should fix your general headache ;-).
But note that, as some programmer dude correctly remarked, standard C++ nevertheless does not permit the definition of arrays whose size is not known at compile time; standard C does though, since C99. The rationale for C++ no following that amendment is that C++ provides better means for the use case, like the vector template. gcc, which is the compiler used in MinGW, permits this as an extension (and why not — it's available in the compiler anyway).
An arguably better alternative in
std::vector
, which is safer, and problems integrating dynamic-sized types into the type-system.– Deduplicator
Aug 29 at 15:13
std::vector
@Deduplicator I thought I said that part with vector ...
– Peter A. Schneider
Aug 29 at 15:27
Hm. Perhaps I should have emphasized the arguably: It's safer but much more expensive. And anyway, the real kicker is the type-system.
– Deduplicator
Aug 29 at 15:42
Because you didn't disable compiler extensions.
– StoryTeller
Aug 29 at 12:43