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.

Recommended Answers

All 6 Replies

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.

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.

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?

If you're worried about it, don't do it.

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/

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.