While trying to explain to someone why a C++ static array could not by dynamically sized, I found gcc disagreeing with me. How does the following code even compile, given that the dimension argc of array is not known at compile time?
#include <iostream>
int main(int argc, char* argv[]) {
int array[argc];
for(int i = 0; i < argc; i++) array[i] = argv[i][0];
for(int i = 0; i < argc; i++) std::cout << i << ": " << char(array[i]) << std::endl;
//for(int i = 0; i < 100; i++) { std::cout << i << " "; std::cout.flush(); array[i] = 0; }
return 0;
}
I tested this with gcc 4.2.1, and specified -Wall, without getting so much as a dirty look from the compiler. If I uncomment the last loop, I get a segfault when I assign to array[53].
I had previously placed guard arrays before and after the declaration of array, and had filled them with zeros, certain that the program must be trashing part of its stack, but gcc reordered the variables on the stack, such that I was unable to observe any data corruption.
Obviously I am not trying to get this code to "work." I'm just trying to understand why gcc even thinks it can compile the code. Any hints or explanations would be much appreciated.
Update: Thanks to all for your helpful and ridiculously fast responses!
Variable-length arrays (VLAs) are part of C99 and have been supported by gcc for a long time:
http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html
Note that the use of VLAs in C90 and C++ code is non-standard, but is supported by gcc as an extension.
That's a Variable Length Array, which is part of the C99 standard. It isn't part of C++ though.
You can also use the alloca function, which also isn't standard C++, but is widely supported:
int main(int argc, char* argv[])
{
int* array = (int*) alloca( argc * sizeof(int) );
array[0] = 123;
// array automatically deallocated here. Don't call free(array)!
}
These are called variable-length arrays (available since C99) and can be declared only as an automatic variable – try putting static in front and the compiler will reject it. It just involves incrementing the stack pointer with a variable rather than with a constant offset, not more than that.
Before the introduction of variable-length arrays, allocation of variable-sized objects on the stack was done with the alloca function.
Variable-sized stack-based arrays are a G++ extension and perfectly legitimate there. They are, however, not Standard. Stack-based arrays can indeed be variably-sized on most implementations, but the Standard does not mandate this.
Arrays in C++ cannot be sized except by using a constant expression. Arrays sized via a non-const are either part of C99 or a horrible extension foisted on us by GCC. You can get rid of most of the GCC crap by using the -pedantic flag when you compile C++ code.
Related
This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 15 days ago.
I've been using C++ for a few years, and today I saw some code, but how can this be perfectly legal?
int main(int argc, char **argv)
{
size_t size;
cin >> size;
int array[size];
for(size_t i = 0; i < size; i++)
{
array[i] = i;
cout << i << endl;
}
return 0;
}
Compiled under GCC.
How can the size be determined at run-time without new or malloc?
Just to double check, I've googled some and all similar codes to mine are claimed to give storage size error.
Even Deitel's C++ How To Program p. 261 states under Common Programming Error 4.5:
Only constants can be used to declare the size of automatic and static arrays.
Enlight me.
This is valid in C99.
C99 standard supports variable sized arrays on the stack. Probably your compiler has chosen to support this construct too.
Note that this is different from malloc and new. gcc allocates the array on the stack, just like it does with int array[100] by just adjusting the stack pointer. No heap allocation is done. It's pretty much like _alloca.
This is known as VLAs (variable length arrays). It is standard in c99, but gcc allows it in c++ code as an extension. If you want it to reject the code, try experimenting with -std=standard, -ansi and -pedantic options.
It is valid only in C99. Next time you may try checking your code in a reliable compiler.
It is valid C99, it is not valid C++. This is one of not a few differences between the two languages.
This Code runs in GNU GCC Compiler.
#include<bits/stdc++.h>
int main(int argc, char **argv)
{
size_t size;
std:: cin >> size;
int array[size];
for(size_t i = 0; i < size; i++)
{
array[i] = i;
std:: cout << i;
}
return 0;
}
You can give size to an array dynamically in if you are using Dev-Cpp compiler I have tried it
and got no error but on visual c++ and visual studio compilers it is not possible.
I think the reason is that dev-c++ assigns a positive number to the uninitialized int
and when we give it a number it is replaced by the given one.
but perhaps other compilers give null to uninitialized variables.
I recently came across a scenario where a stack-allocated array is desired. (It's a wrapper around v8, needed an array of args on every method call).
An std::vector would do heap memory allocation, whose performance is not acceptable.
Here is my solution, use template to allocation array of cases:
template<size_t Argc>
static void call(...) {
v8::Local<v8::Value> v8Args[Argc];
// use v8Args
...
}
template<typename It>
static void callV8Function(size_t argc, It argvBegin, It argvEnd,) {
// C++ don't have dynamic stack allocation (like C99 does)
// try to avoid heap-allocation...
if (argc <= 4) {
return callV8FunctionOnStack<4>(...);
} else if (argc <= 8) {
return callV8FunctionOnStack<8>(...);
} else if (argc <= 16) {
return callV8FunctionOnStack<16>(...);
} else if (argc <= 32) {
return callV8FunctionOnStack< 32>(...);
} else {
std::vector<v8::Local<v8::Value>> v8Args(argc);
// fallback to vector
}
}
(And of course, I can just do with a 32-sized array, but which is not so elegant.)
Variable Length Arrays (VLAs) are supported in the C++14 standard, which has recently been accepted, and is awaiting publication.
This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 17 days ago.
I've been using C++ for a few years, and today I saw some code, but how can this be perfectly legal?
int main(int argc, char **argv)
{
size_t size;
cin >> size;
int array[size];
for(size_t i = 0; i < size; i++)
{
array[i] = i;
cout << i << endl;
}
return 0;
}
Compiled under GCC.
How can the size be determined at run-time without new or malloc?
Just to double check, I've googled some and all similar codes to mine are claimed to give storage size error.
Even Deitel's C++ How To Program p. 261 states under Common Programming Error 4.5:
Only constants can be used to declare the size of automatic and static arrays.
Enlight me.
This is valid in C99.
C99 standard supports variable sized arrays on the stack. Probably your compiler has chosen to support this construct too.
Note that this is different from malloc and new. gcc allocates the array on the stack, just like it does with int array[100] by just adjusting the stack pointer. No heap allocation is done. It's pretty much like _alloca.
This is known as VLAs (variable length arrays). It is standard in c99, but gcc allows it in c++ code as an extension. If you want it to reject the code, try experimenting with -std=standard, -ansi and -pedantic options.
It is valid only in C99. Next time you may try checking your code in a reliable compiler.
It is valid C99, it is not valid C++. This is one of not a few differences between the two languages.
This Code runs in GNU GCC Compiler.
#include<bits/stdc++.h>
int main(int argc, char **argv)
{
size_t size;
std:: cin >> size;
int array[size];
for(size_t i = 0; i < size; i++)
{
array[i] = i;
std:: cout << i;
}
return 0;
}
You can give size to an array dynamically in if you are using Dev-Cpp compiler I have tried it
and got no error but on visual c++ and visual studio compilers it is not possible.
I think the reason is that dev-c++ assigns a positive number to the uninitialized int
and when we give it a number it is replaced by the given one.
but perhaps other compilers give null to uninitialized variables.
I recently came across a scenario where a stack-allocated array is desired. (It's a wrapper around v8, needed an array of args on every method call).
An std::vector would do heap memory allocation, whose performance is not acceptable.
Here is my solution, use template to allocation array of cases:
template<size_t Argc>
static void call(...) {
v8::Local<v8::Value> v8Args[Argc];
// use v8Args
...
}
template<typename It>
static void callV8Function(size_t argc, It argvBegin, It argvEnd,) {
// C++ don't have dynamic stack allocation (like C99 does)
// try to avoid heap-allocation...
if (argc <= 4) {
return callV8FunctionOnStack<4>(...);
} else if (argc <= 8) {
return callV8FunctionOnStack<8>(...);
} else if (argc <= 16) {
return callV8FunctionOnStack<16>(...);
} else if (argc <= 32) {
return callV8FunctionOnStack< 32>(...);
} else {
std::vector<v8::Local<v8::Value>> v8Args(argc);
// fallback to vector
}
}
(And of course, I can just do with a 32-sized array, but which is not so elegant.)
Variable Length Arrays (VLAs) are supported in the C++14 standard, which has recently been accepted, and is awaiting publication.
This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 17 days ago.
I've been using C++ for a few years, and today I saw some code, but how can this be perfectly legal?
int main(int argc, char **argv)
{
size_t size;
cin >> size;
int array[size];
for(size_t i = 0; i < size; i++)
{
array[i] = i;
cout << i << endl;
}
return 0;
}
Compiled under GCC.
How can the size be determined at run-time without new or malloc?
Just to double check, I've googled some and all similar codes to mine are claimed to give storage size error.
Even Deitel's C++ How To Program p. 261 states under Common Programming Error 4.5:
Only constants can be used to declare the size of automatic and static arrays.
Enlight me.
This is valid in C99.
C99 standard supports variable sized arrays on the stack. Probably your compiler has chosen to support this construct too.
Note that this is different from malloc and new. gcc allocates the array on the stack, just like it does with int array[100] by just adjusting the stack pointer. No heap allocation is done. It's pretty much like _alloca.
This is known as VLAs (variable length arrays). It is standard in c99, but gcc allows it in c++ code as an extension. If you want it to reject the code, try experimenting with -std=standard, -ansi and -pedantic options.
It is valid only in C99. Next time you may try checking your code in a reliable compiler.
It is valid C99, it is not valid C++. This is one of not a few differences between the two languages.
This Code runs in GNU GCC Compiler.
#include<bits/stdc++.h>
int main(int argc, char **argv)
{
size_t size;
std:: cin >> size;
int array[size];
for(size_t i = 0; i < size; i++)
{
array[i] = i;
std:: cout << i;
}
return 0;
}
You can give size to an array dynamically in if you are using Dev-Cpp compiler I have tried it
and got no error but on visual c++ and visual studio compilers it is not possible.
I think the reason is that dev-c++ assigns a positive number to the uninitialized int
and when we give it a number it is replaced by the given one.
but perhaps other compilers give null to uninitialized variables.
I recently came across a scenario where a stack-allocated array is desired. (It's a wrapper around v8, needed an array of args on every method call).
An std::vector would do heap memory allocation, whose performance is not acceptable.
Here is my solution, use template to allocation array of cases:
template<size_t Argc>
static void call(...) {
v8::Local<v8::Value> v8Args[Argc];
// use v8Args
...
}
template<typename It>
static void callV8Function(size_t argc, It argvBegin, It argvEnd,) {
// C++ don't have dynamic stack allocation (like C99 does)
// try to avoid heap-allocation...
if (argc <= 4) {
return callV8FunctionOnStack<4>(...);
} else if (argc <= 8) {
return callV8FunctionOnStack<8>(...);
} else if (argc <= 16) {
return callV8FunctionOnStack<16>(...);
} else if (argc <= 32) {
return callV8FunctionOnStack< 32>(...);
} else {
std::vector<v8::Local<v8::Value>> v8Args(argc);
// fallback to vector
}
}
(And of course, I can just do with a 32-sized array, but which is not so elegant.)
Variable Length Arrays (VLAs) are supported in the C++14 standard, which has recently been accepted, and is awaiting publication.
I'm surprised that this code compiles and works perfectly without throwing any errors!
int arraysize = 1000;
int array[arraysize];
for(int i=0; i<arraysize; i++)
{
array[i] = i+1;
}
for(int i=0; i<arraysize; i++)
{
cout << array[i];
}
Edit: Compiler used: i386-linux-gnueabi-g++ (Linaro GCC 4.5-2012.01) 4.5.4 20120105 (prerelease)
In C++ the size of an array must be a constant. If you were to declare the size variable const, then it could be used.
C allows variable-length arrays (sometimes called VLAs), and some C++ compilers provide these as an extension; that would be why your code works.
Usually, std::vector is a safer and more portable alternative if you need a dynamically-sized array.
This is probably a feature of your compiler (GCC ?) which allows C99 variable-length arrays. In C99, it's valid to define arrays such as
int n;
scanf("%d", &n);
int array[n];
C++ does not, by standard, support variable-length arrays, probably because it has better alternatives, namely std::vector<>. Try compiling with g++ -pedantic-errors file.cpp and you'll receive
error: ISO C++ forbids variable-size array ‘array’
It should be noted that variable-length arrays do not support C++ classes, which is another reason not to bother with them in C++, and instead using std::vector<>.
This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 20 days ago.
I've been using C++ for a few years, and today I saw some code, but how can this be perfectly legal?
int main(int argc, char **argv)
{
size_t size;
cin >> size;
int array[size];
for(size_t i = 0; i < size; i++)
{
array[i] = i;
cout << i << endl;
}
return 0;
}
Compiled under GCC.
How can the size be determined at run-time without new or malloc?
Just to double check, I've googled some and all similar codes to mine are claimed to give storage size error.
Even Deitel's C++ How To Program p. 261 states under Common Programming Error 4.5:
Only constants can be used to declare the size of automatic and static arrays.
Enlight me.
This is valid in C99.
C99 standard supports variable sized arrays on the stack. Probably your compiler has chosen to support this construct too.
Note that this is different from malloc and new. gcc allocates the array on the stack, just like it does with int array[100] by just adjusting the stack pointer. No heap allocation is done. It's pretty much like _alloca.
This is known as VLAs (variable length arrays). It is standard in c99, but gcc allows it in c++ code as an extension. If you want it to reject the code, try experimenting with -std=standard, -ansi and -pedantic options.
It is valid only in C99. Next time you may try checking your code in a reliable compiler.
It is valid C99, it is not valid C++. This is one of not a few differences between the two languages.
This Code runs in GNU GCC Compiler.
#include<bits/stdc++.h>
int main(int argc, char **argv)
{
size_t size;
std:: cin >> size;
int array[size];
for(size_t i = 0; i < size; i++)
{
array[i] = i;
std:: cout << i;
}
return 0;
}
You can give size to an array dynamically in if you are using Dev-Cpp compiler I have tried it
and got no error but on visual c++ and visual studio compilers it is not possible.
I think the reason is that dev-c++ assigns a positive number to the uninitialized int
and when we give it a number it is replaced by the given one.
but perhaps other compilers give null to uninitialized variables.
I recently came across a scenario where a stack-allocated array is desired. (It's a wrapper around v8, needed an array of args on every method call).
An std::vector would do heap memory allocation, whose performance is not acceptable.
Here is my solution, use template to allocation array of cases:
template<size_t Argc>
static void call(...) {
v8::Local<v8::Value> v8Args[Argc];
// use v8Args
...
}
template<typename It>
static void callV8Function(size_t argc, It argvBegin, It argvEnd,) {
// C++ don't have dynamic stack allocation (like C99 does)
// try to avoid heap-allocation...
if (argc <= 4) {
return callV8FunctionOnStack<4>(...);
} else if (argc <= 8) {
return callV8FunctionOnStack<8>(...);
} else if (argc <= 16) {
return callV8FunctionOnStack<16>(...);
} else if (argc <= 32) {
return callV8FunctionOnStack< 32>(...);
} else {
std::vector<v8::Local<v8::Value>> v8Args(argc);
// fallback to vector
}
}
(And of course, I can just do with a 32-sized array, but which is not so elegant.)
Variable Length Arrays (VLAs) are supported in the C++14 standard, which has recently been accepted, and is awaiting publication.