Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Dynamic sized array?

Reply
Thread Tools

Dynamic sized array?

 
 
Daniel T.
Guest
Posts: n/a
 
      10-17-2006
I recently came across something like the following:

int main() {
unsigned i;
cin >> i;
int arr[i];
// use arr
}

Is that valid in C++? I thought it was only valid in C99...

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-17-2006
Daniel T. wrote:

> I recently came across something like the following:
>
> int main() {
> unsigned i;
> cin >> i;
> int arr[i];
> // use arr
> }
>
> Is that valid in C++?


No. However, some compiler accept it as an extension. However, a diagnostic
is required: if the compiler accepts it, you should still get a warning.


> I thought it was only valid in C99...


I don't know about C99.


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
Martin Steen
Guest
Posts: n/a
 
      10-17-2006
Daniel T. wrote:
> I recently came across something like the following:
>
> int main() {
> unsigned i;
> cin >> i;
> int arr[i];
> // use arr
> }
>
> Is that valid in C++? I thought it was only valid in C99...



No, it's not valid. Although some compilers (e.g. g++) may
compile it.

If you want dynamic arrays, better use vector-templates from the STL:

#include <vector>

int main()
{
unsigned i;
cin >> i;
std::vector<int> arr(i);
// use arr
return 0;
}

-Martin
 
Reply With Quote
 
Sumit Rajan
Guest
Posts: n/a
 
      10-17-2006
Daniel T. wrote:
> I recently came across something like the following:
>
> int main() {
> unsigned i;
> cin >> i;
> int arr[i];
> // use arr
> }
>
> Is that valid in C++?


No/Not yet.

>I thought it was only valid in C99...


Yes, it is valid in C99.

Regards,
Sumit.
 
Reply With Quote
 
Sumit Rajan
Guest
Posts: n/a
 
      10-17-2006
Sumit Rajan wrote:

> Yes, it is valid in C99.


Of course, "cin" wouldn't be around in such a case.

Sumit.
 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struct with dynamic sized array member joe C++ 1 08-31-2011 09:02 AM
List as a dynamic array of increasing-sized arrays MartinBroadhurst C Programming 13 11-03-2010 12:05 PM
Oddly sized sized password textbox thechaosengine ASP .Net 1 09-15-2005 07:34 PM
defining dynamic sized tables in XML schema... obsolete_5@hotmail.com XML 1 05-31-2005 11:10 AM
Dynamic sized structs Jonathan Halterman C++ 2 10-15-2003 01:10 AM



Advertisments