Why is it allowed to declare an automatic array with size depending on user input? [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
11
down vote
favorite
This question already has an answer here:
Why aren't variable-length arrays part of the C++ standard?
13 answers
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?
c++ arrays stack variable-length-array
marked as duplicate by SHR, Edgar RokjÃÂn
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Aug 29 at 16:08
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.
 |Â
show 3 more comments
up vote
11
down vote
favorite
This question already has an answer here:
Why aren't variable-length arrays part of the C++ standard?
13 answers
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?
c++ arrays stack variable-length-array
marked as duplicate by SHR, Edgar RokjÃÂn
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Aug 29 at 16:08
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.
10
Because you didn't disable compiler extensions.
â StoryTeller
Aug 29 at 12:43
5
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
1
Note, this is an automatic array, not a static one
â M.M
Aug 29 at 14:25
1
@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
 |Â
show 3 more comments
up vote
11
down vote
favorite
up vote
11
down vote
favorite
This question already has an answer here:
Why aren't variable-length arrays part of the C++ standard?
13 answers
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?
c++ arrays stack variable-length-array
This question already has an answer here:
Why aren't variable-length arrays part of the C++ standard?
13 answers
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 already has an answer here:
Why aren't variable-length arrays part of the C++ standard?
13 answers
c++ arrays stack variable-length-array
edited Aug 29 at 14:57
Deduplicator
33.1k64786
33.1k64786
asked Aug 29 at 12:42
Floella
493416
493416
marked as duplicate by SHR, Edgar RokjÃÂn
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Aug 29 at 16:08
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.
marked as duplicate by SHR, Edgar RokjÃÂn
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Aug 29 at 16:08
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.
10
Because you didn't disable compiler extensions.
â StoryTeller
Aug 29 at 12:43
5
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
1
Note, this is an automatic array, not a static one
â M.M
Aug 29 at 14:25
1
@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
 |Â
show 3 more comments
10
Because you didn't disable compiler extensions.
â StoryTeller
Aug 29 at 12:43
5
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
1
Note, this is an automatic array, not a static one
â M.M
Aug 29 at 14:25
1
@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
10
10
Because you didn't disable compiler extensions.
â StoryTeller
Aug 29 at 12:43
Because you didn't disable compiler extensions.
â StoryTeller
Aug 29 at 12:43
5
5
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
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
see Does âÂÂint size = 10;â yield a constant expression?
â Shafik Yaghmour
Aug 29 at 13:04
1
1
Note, this is an automatic array, not a static one
â M.M
Aug 29 at 14:25
Note, this is an automatic array, not a static one
â M.M
Aug 29 at 14:25
1
1
@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
@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
 |Â
show 3 more comments
3 Answers
3
active
oldest
votes
up vote
19
down vote
It's not. C++ doesn't have variable-length arrays, though some compilers allow it as an extension of the language.
add a comment |Â
up vote
15
down vote
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:
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.
4
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
add a comment |Â
up vote
3
down vote
[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 instd::vector
, which is safer, and problems integrating dynamic-sized types into the type-system.
â Deduplicator
Aug 29 at 15:13
@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
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
19
down vote
It's not. C++ doesn't have variable-length arrays, though some compilers allow it as an extension of the language.
add a comment |Â
up vote
19
down vote
It's not. C++ doesn't have variable-length arrays, though some compilers allow it as an extension of the language.
add a comment |Â
up vote
19
down vote
up vote
19
down vote
It's not. C++ doesn't have variable-length arrays, though some compilers allow it as an extension of the language.
It's not. C++ doesn't have variable-length arrays, though some compilers allow it as an extension of the language.
answered Aug 29 at 12:44
Some programmer dude
281k23227381
281k23227381
add a comment |Â
add a comment |Â
up vote
15
down vote
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:
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.
4
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
add a comment |Â
up vote
15
down vote
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:
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.
4
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
add a comment |Â
up vote
15
down vote
up vote
15
down vote
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:
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.
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:
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.
answered Aug 29 at 14:01
Carsten
1687
1687
4
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
add a comment |Â
4
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
4
4
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
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
add a comment |Â
up vote
3
down vote
[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 instd::vector
, which is safer, and problems integrating dynamic-sized types into the type-system.
â Deduplicator
Aug 29 at 15:13
@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
add a comment |Â
up vote
3
down vote
[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 instd::vector
, which is safer, and problems integrating dynamic-sized types into the type-system.
â Deduplicator
Aug 29 at 15:13
@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
add a comment |Â
up vote
3
down vote
up vote
3
down vote
[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).
[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).
answered Aug 29 at 15:02
Peter A. Schneider
9,0371741
9,0371741
An arguably better alternative instd::vector
, which is safer, and problems integrating dynamic-sized types into the type-system.
â Deduplicator
Aug 29 at 15:13
@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
add a comment |Â
An arguably better alternative instd::vector
, which is safer, and problems integrating dynamic-sized types into the type-system.
â Deduplicator
Aug 29 at 15:13
@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
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
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
@Deduplicator I thought I said that part with vector ...
â Peter A. Schneider
Aug 29 at 15:27
@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
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
add a comment |Â
10
Because you didn't disable compiler extensions.
â StoryTeller
Aug 29 at 12:43
5
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
1
Note, this is an automatic array, not a static one
â M.M
Aug 29 at 14:25
1
@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