Connecting Tech Pros Worldwide Forums | Help | Site Map

Why does this compile in g++ 3.2 and greater?

merk
Guest
 
Posts: n/a
#1: Sep 28 '06
I always understood that an array had to have a defined size at compile
time via either a real value or a const so why does this work? Note
that it does fail in MS Visual Studio 2003.

#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;
}

Thanks,
merk


Thomas J. Gritzan
Guest
 
Posts: n/a
#2: Sep 28 '06

re: Why does this compile in g++ 3.2 and greater?


merk schrieb:
Quote:
I always understood that an array had to have a defined size at compile
time via either a real value or a const so why does this work? Note
that it does fail in MS Visual Studio 2003.
>
#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

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Jens Theisen
Guest
 
Posts: n/a
#3: Sep 28 '06

re: Why does this compile in g++ 3.2 and greater?


"merk" <kgmerk@gmail.comwrites:
Quote:
I always understood that an array had to have a defined size at compile
time via either a real value or a const so why does this work? Note
that it does fail in MS Visual Studio 2003.
It's C99, and gcc allows it as an extension in C++.

Regards,

Jens
Earl Purple
Guest
 
Posts: n/a
#4: Sep 29 '06

re: Why does this compile in g++ 3.2 and greater?



Thomas J. Gritzan wrote:
Quote:
merk schrieb:
Quote:
#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.

Thomas J. Gritzan
Guest
 
Posts: n/a
#5: Sep 29 '06

re: Why does this compile in g++ 3.2 and greater?


Earl Purple schrieb:
Quote:
Thomas J. Gritzan wrote:
Quote:
>merk schrieb:
Quote:
>>#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
Closed Thread