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

M

merk

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
 
T

Thomas J. Gritzan

merk said:
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
 
J

Jens Theisen

merk said:
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
 
E

Earl Purple

Thomas said:
merk said:
#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.
 
T

Thomas J. Gritzan

Earl said:
Thomas said:
merk said:
#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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top