473,394 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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

Sep 28 '06 #1
4 1672
merk schrieb:
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
Sep 28 '06 #2
"merk" <kg****@gmail.comwrites:
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
Sep 28 '06 #3

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.

Sep 29 '06 #4
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
Sep 29 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gary Quiring | last post by:
I am trying to compile PHP 5.0.4 on Solaris 9 with GMP. I keep getting this error: configure: error: GNU MP Library version 4.1.2 or greater required. I installed GMP 4.1.4 using: ./configure...
12
by: Helmut Jarausch | last post by:
Hi, what does Python do if two objects aren't comparable (to my opinion) If I've understood "Python in a Nutschell" correctly it should raise an exception but it doesn't do for me. Here are...
35
by: Ram Laxman | last post by:
I have used vector in the VC++ compiler. I have included #include <string> #include <algorithm> #include <vector> std::vector<int> field; std::vector <int>::size_type i;
34
by: Sunner Sun | last post by:
Hi! I have looked through the FAQ but found nothing about it. :-) It seems that this kind of macro is platform dependent, doesn't it? Thank you. Sunner Sun
3
by: Frodo Baggins | last post by:
Hi All, I have a piece of code (not written by me) that is failing on compile with the error: pasting "xdr_ndmp_connect_open_request" and "," does not give a valid preprocessing token The...
29
by: Vol | last post by:
I think 'atan' can get the angle but it is not the four quadrant angle. Is there any function that i can get the angle from -pi to pi? or I have to use some if ... else? I know in Matlab, we use...
1
by: laredotornado | last post by:
Hello, Given two strings, a and b, how would I determine if a is alphabetically greater than or less than b? I'm using PHP 4. Thanks, - Dave
3
thatos
by: thatos | last post by:
I would like to design a method which when given an array it returns an array which it's elements are from the initial array greater than the average of all numbers in that array; the method...
4
by: Daniel Pitts | last post by:
I have a template: template<typename c, unsigned size> struct Vector { c components; template<unsigned index> c &get() { return components; } }; Vector<double, 3vect;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.