Reputation: 930
I'm new to C++ and this is a very basic question.
In C++ there are only two ways to create dynamic arrays (read in a book, correct me if I'm wrong) using memory allocation either by new
operator or malloc()
function which is taken from C.
When declaring an array int array[size]
, the square brackets []
must have a const
.
However in the following code size
is an unsigned int
variable.
#include<iostream>
int main() {
using namespace std;
unsigned int size;
cout<<"Enter size of the array : ";
cin>>size;
int array[size]; // Dynamically Allocating Memory
cout<<"\nEnter the elements of the array\n";
// Reading Elements
for (int i = 0; i < size; i++) {
cout<<" :";
cin>>array[i];
}
// Displaying Elements
cout<<"\nThere are total "<<size<<" elements, as listed below.";
for (int j = 0; j < size; j++) {
cout<<endl<<array[j];
}
return 0;
}
While compiling g++ throws no error and moreover the program runs perfectly.
Question 1 : Could this be another way to create dynamic array?
Question 2 : Is the code correct?
Question 3 : If []
can only contain const
, why does the code work?
Upvotes: 7
Views: 7457
Reputation: 101456
In C++ there are only two ways to create dynamic arrays (read in a book, correct me if I'm wrong) using memory allocation either by new operator or malloc() function which is taken from C.
This depends on what you mean by dynamic.
From a strictly technical perspective, in the context of the lifetime of an object, dynamic means something with dynamic storage duration. A storage duration is how long an object will live after it has been created. An object that has dynamic storage duration will continue to live on until it is explicitly destroyed. (Note that this explicit destruction may be obfuscated through the use of smart pointers such as std::unique_ptr
)
However this does not seem to be what you're really asking about in this question. You seem to be asking:
If I want to create a container for objects, but I don't know how big that container needs to be until runtime, is
new
andmalloc
my only choice?
The answer to that question is no. And in fact, in modern C++ using new
should be a choice of last resort, and using malloc
should be even more last-resort than that.
Instead of trying to create a C-style array of N
elements, use a vector
instead:
std::vector <int> myInts;
for (int i = 0; i < 10; ++i)
myInts.push_back (i);
This has several advantages:
delete
anythingvector
before you create the vector
-- just keep adding elements until you're donevector
is guaranteed to have contigious storage, meaning the elements can be accessed using operator[]
just like a C-style arrayUpvotes: 1
Reputation: 726479
gcc
extension. This has been allowed in C for a long time, but it has not made it into C++ standard until the C++14.gcc
that you do not want to use extensions by supplying a -std=c++98
or -std=c++11
compiler flag.If you need a dynamically-sized array in C++, a better approach would be to use std::vector<T>
. It gives you the flexibility of dynamically allocated array, and takes care of managing the allocated memory for you.
Upvotes: 6