I have a few array-related questions. I've studied that array-size must be constant on declaration/compiler must know its value. But using the GNU GCC compiler (C++11 standard filter) and I am able to perfectly compile and run a program using a variable as array size, when declaring said array dynamically (using new
)
int num;
cout << "How big an array? ";
cin >> num;
int *arr = new int [num];
Ques1) Is this considered standard? My profs are contradictory.
Ques2) If it is standard, in that case, is it possible to extend the size of the array (or any array) after creation?
Ques3) Again, if this expression is standard, then is it possible to use it within a function - eg. using a function to create such an array? (if so, how?)
(PS: Hi, I'm new here and also still a novice in C++)
Question 1 - operator 'new' is used to make dynamic allocation, I mean, when you don't know previously what is the size of the array, so, there is no problem, you can do it! I think your profs are confusing with C sintax, where new neither exists and is not allowed to make things like: int p[n]; for instance.
Question 2 - No, it is not possible increase the size of an array created using operator new. You have to alocate another array and copy the data. You can consider use vector in order to do it easily.
Question 3 - I don't see why to do it, but it is possible..
Q1:Is this considered standard?
Q2:If it is standard, in that case, is it possible to extend the size of the array (or any array) after creation?
Partially no, or not recommended.
when the allocation function returns a value other than null, it must be a pointer to a block of storage in which space for the object has been reserved. Mostly allocation happened in heap, and there may not have more contiguous memory left which is important for array.
If you have to do this and have a memory poll, use placement new operator you can do this partially, but what you do now is what the designer of allocator do, and have risk ruin the inner memory storage.
Q3: using a function to create such an array? (if so, how?)
The rest of things are how to design such function to meet your need, even use template.
Well, that's true, but only for static or automatic arrays. You are allocating a dynamic array on the heap, which is different.
Static array
An array declared at global scope must have a constant size.
Automatic array
An array allocated automatically within a function must have constant size (with exception, see below).
Dynamic array
A dynamic array allocated on the heap with
new
can have any size, constant or variable.GCC extension
The exception is that GCC allows one to use a variable to declare the size of an automatic array:
However, this usage is not standard.
Yes, this is completely valid. Note that you need to explicitly delete the memory pointed by
arr
using operatordelete[]
. It is much better to use astd::vector<int>
which will perform the memory management for you.You might be mistaken with variable length arrays(VLAs), which are not allowed in
C++
:This is valid in
C
but invalid inC++
(C++14 will include VLAs, although they will have some differences withC
VLAs).No, you can't extend it. You can allocate a bigger array, copy the elements and delete the previous one. Again, this is done automatically if you're using
std::vector<int>
.Yes, of course:
But again use
std::vector
:As a complement to other answers :
Vectors
(Agreed with vector for dynamic resize) :
Arrays
Side note : (about stack and heap allocation)
The way an
array
is handled can lead to significantly different results (See this very interesting discussion):