Earl Purple schrieb:
Thomas J. Gritzan wrote:
>merk schrieb:
>>#include <iostream>
int main()
{
int n;
std::cin >n;
double a[n][n];
a[0][0] = 1.0;
a[0][1] = 2.0;
std::cout << a[0][0] << " " << a[0][1] << std::endl;
}
It is a GCC extension:
http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html
Not a bad idea for an extension to the language either. Internally I
assume would be equivalent to a dynamic allocation with RAII, a bit
like boost::scoped_array (if such exists, else scoped_ptr with array
deleter) except that is allows multi-dimensions. Would make a good
addition to the C++ standard. You wouldn't be allowed to return it from
a function though and trying to do so would hopefully lead to a
compiler warning.
I think it is equivalent to using the alloca() function, reserving space in
the stack frame (for implementations with a stack). That would make it as
fast as constant size arrays in automatic storage. However, reserving more
space than available wouldn't throw an exception like new[] does but would
invoke undefined behaviour, so that std::vector should be preferred in general.
Since it is in the C99 standard, it will possibly make it into the C++0x
standard for compatibility reasons.
--
Thomas
http://www.netmeister.org/news/learn2quote.html