Now let's get back to arrays.
In one of the previous lessons,
I took a little bit of time to explain why the size of an
array needs to be known at compile time.
Since that's just how static arrays work,
the compiler needs to know how big the array is going to be so that it
knows how to manage stack memory and optimize it.
But this is very limiting.
What if I want to let the user specify the size of the array?
I created an integer variable,
which will be initialized by the input from the terminal,
and then I will use that variable to set the size of the array.
Let's compile this.
I need to specify the size, so let's say 5.
And it seems that this worked.
No errors or warnings.
So why did I have that conspiracy theory about knowing the size at compile time?
Unfortunately, sometimes this will work, depending on which compiler you use.
This feature of declaring the array size from a variable
is known as variable length arrays, or VLA for short.
C++ does not support VLAs in its language standard.
The reason that this worked for me is because my new
compiler has an extension for compiling C code,
and C version 99 does have a support for variable length arrays.
But you shouldn't rely on this feature because it
will not work with all compilers,
and using non‑standard C++ code will affect the portability of your programs.
I will add this special flag to the command to let the compiler
know that it should treat VLAs as an error.
And now if I try to compile this, array declaration will result in an error,
as it should, because we don't want them in C++.
If we want to create arrays with dynamic size, we can allocate them on the heap.
Let's do that in our calculator program.
I will remove this SIZE constant and create a non‑constant size variable,
which will store the input from the user.
We will need another message to ask the user about the number of
operands and receive the input as a response,
and change this size to lowercase because we don't use the constant anymore.
I just want to mention that one possible solution is
to make this array really large.
For example, we can make it store 100 elements.
That way, we will have a static array with a lot of space,
and we can only use the number of elements that we
actually need to store the operands.
This would work, but in real projects this is a really big waste of space,
especially if we know that we're not going to need so much elements.
Instead,
I want to only allocate the amount of space in memory that we actually need.
There is a special version of the new operator that
supports the creation of arrays on the heap.
You specify the size of the array inside of the square brackets and
put the data type of these elements next to it.
This will allocate an array on the heap.
Of course, since this is allocated on the heap,
we need to store the memory address in some pointer.
Now you can see why I said that using size of operator to
determine the number of elements is bad.
If you try to do that with this heap‑allocated array,
it wouldn't work because this nums is just a pointer
to a memory address on the heap.
It makes sense that we are able to create dynamically sized arrays on the heap
because heap provides us with dynamic memory allocation.
It literally allocates memory at runtime and not at compile time.
The rest of the code can still stay the same.
We just need to replace this SIZE constant with size variable in a few places.
Once we do that, everything else should work like it used to. However,
if you paid attention in the previous module,
you might be wondering if this would cause a memory leak.
After all, we are allocating memory on the heap, but we are not releasing it.
And you're right. We do need to release allocated memory, so I will use the
delete operator at the end of the main function.
One thing to note is that if you allocate an array on the
heap with this new square brackets operator,
you also need to deallocated with delete[] operator.
This is because the delete needs to loop through the array and call
the destructor of each object, if one is provided,
of course.
Okay, now let's compile this to see if the program still works.
I am asked to provide the number of operands. I will choose 3.
Let's pick some numbers and choose the plus operator.
And yes, everything still works,
but now the user can specify the number of operands, and no
modifications of the code itself are required. So if you want to
dynamically set the size of an array, use heap. This is actually a
common use case for heap memory,
especially in the old days when we didn't have all of those fancy classes
from C++, but we will talk about these classes later.