473,394 Members | 1,843 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.

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.
Oct 17 '06 #1
4 1276
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<intarr(i);
// use arr
return 0;
}

-Martin
Oct 17 '06 #2
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.
Oct 17 '06 #3
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
Oct 17 '06 #4
Sumit Rajan wrote:
Yes, it is valid in C99.
Of course, "cin" wouldn't be around in such a case.

Sumit.
Oct 17 '06 #5

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

Similar topics

19
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at...
15
by: Ronny Mandal | last post by:
Assume that you want to store n records in an array, and that the array can hold all n elements, that is, it is declared int a. And suddenlny in run-time, I need to store n+m items. Why cannot...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
3
by: Nikesh | last post by:
project is abt encrypting a txt file with an image.... in that i will to accept a txt file from the user ..this file a need to be stored in an array...thus file size will keep on changing....and...
14
by: romayankin | last post by:
Hello All, I'm writing cross-platform code so i'm bound to standards. Here is the code I have: ~~~~~~~~~~~~~~~~~~ double **mx = new double*; for(int i = 0; i < col - 1; i++) { mx = new...
10
by: Zytan | last post by:
This article: http://www.c-sharpcorner.com/UploadFile/mahesh/WorkingWithArrays11142005060354AM/WorkingWithArrays.aspx claims that "In C#, arrays can be declared as fixed length or dynamic". I...
13
by: kwikius | last post by:
Does anyone know what a C99 dynamic array is, and if it will be useable in C++? regards Andy Little
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
14
by: mojumbo | last post by:
Problem: I have a structure which needs to store its data in contiguous memory by there is a dynamic element which can't be defined at compile time. It needs to be aligned along a 4 byte...
3
by: Charming12 | last post by:
Hi All, This is a problem which is eating my head from several days. I have a structure which contains an array of elements as public byte pinNumbers;
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.