| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Daniel T." |
| Date: |
17 Oct 2006 06:48:44 AM |
| Object: |
Dynamic sized array? |
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.
.
|
|
| User: "Kai-Uwe Bux" |
|
| Title: Re: Dynamic sized array? |
17 Oct 2006 07:10:07 AM |
|
|
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
.
|
|
|
|
| User: "Sumit Rajan" |
|
| Title: Re: Dynamic sized array? |
17 Oct 2006 07:13:59 AM |
|
|
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.
.
|
|
|
| User: "Sumit Rajan" |
|
| Title: Re: Dynamic sized array? |
17 Oct 2006 07:19:03 AM |
|
|
Sumit Rajan wrote:
Yes, it is valid in C99.
Of course, "cin" wouldn't be around in such a case.
Sumit.
.
|
|
|
|
|
| User: "Martin Steen" |
|
| Title: Re: Dynamic sized array? |
17 Oct 2006 07:10:50 AM |
|
|
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
.
|
|
|
|

|
Related Articles |
|
|