Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 28th, 2006, 05:25 PM
merk
Guest
 
Posts: n/a
Default 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

  #2  
Old September 28th, 2006, 05:25 PM
Thomas J. Gritzan
Guest
 
Posts: n/a
Default 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
  #3  
Old September 28th, 2006, 11:45 PM
Jens Theisen
Guest
 
Posts: n/a
Default 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
  #4  
Old September 29th, 2006, 09:15 AM
Earl Purple
Guest
 
Posts: n/a
Default 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.

  #5  
Old September 29th, 2006, 03:45 PM
Thomas J. Gritzan
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles