0

Hi I am new to C++ and I just need some advice on declaring dynamic arrays.

Take for example this code block:
int a=0;
cout << "Enter Arr size: ";
cin >> a;
int arr[a];

My compiler gives no error doing this but looking around the web I see many articles saying I should be using the new and delete commands to allocate arrays dynamically. My main question is if this is ok to do? Or will I run into problems in the future.

Thank you in advance for your help.

3
Contributors
6
Replies
7
Views
4 Years
Discussion Span
Last Post by Kamina00
0

Also have another question not sure how to edit previous post.

Anyway I was looking at this link http://www.cplusplus.com/reference/stl/array/size/
and when I use the same sample code as the one provided I get this error:

array' is not a member ofstd'

I have tried to remove the std:: before and the error is then:

`array' undeclared (first use this function)

Is there an issue with #include <array>? I am not really sure what is wrong here.

EDIT: Nevermind ignore this question I figured out that I don't even have array.h in my libraries thats the issue.

Edited by Kamina00

0

My compiler gives no error doing this but looking around the web I see many articles saying I should be using the new and delete commands to allocate arrays dynamically. My main question is if this is ok to do? Or will I run into problems in the future.

Being able to declare an array based on an input value is new to C++ so it really depends on what compiler you are using and which ones you use in the future. Some have that capability now, others don't.

0
Being able to declare an array based on an input value is new to C++ so it really depends on what compiler you are using and which ones you use in the future. Some have that capability now, others don't.

Thank you.

I am using Dev-CPP 4.9.9.2 with GCC version 3.4.2-20040916-1.

This is a pretty old IDE and compiler version after checking the latest versions online. How long has C++ supported this? I am just curious in case I need to compile this on another machine will I run into trouble?

Edited by Kamina00

0

How long has C++ supported this?

C++ has never supported this. It wasn't supported in 1998 and it still isn't supported today. Variable length array is a C99 feature.

In C++, use std::vector<> if similar functionality is required.
http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

I am using Dev-CPP 4.9.9.2 with GCC version 3.4.2-20040916-1.

You should switch to a more recent compiler. If you would like to continue using the familiar IDE, Orwell Dev-C++ would be a reasonable replacement:
http://sourceforge.net/projects/orwelldevcpp/

Edited by vijayan121

0

Thank you for your advice. Still scratching my head abit but this will do I suppose.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.