The way I see it, you still have to declare a size of the array whether it's temporary or permanent. |
Correct. And helios is correct; you can't declare an array of unknown size, whether dynamic or not.
int arr* = new int[?]
Some number has to go where that question mark is. Can't leave it empty.
Perhaps I misunderstand, but to me it seems like you're comparing apples to oranges with your two code snippets. One is file IO controlled, the other is user IO controlled. Two different applications, two different purposes.
One isn't better than the other, it depends on how you want your program to behave. (Edit: Actually your first example is illegal because you're using a VLA. See below, and see Yanson's answer.)
DaRealFonz wrote: |
---|
So if someone can please clarify to me the advantages of using pointers for a dynamic array vs using just a plain array |
I think there's some conflicting terminology you're dealing with here.
First there are regular C arrays.
1 2 3
|
const int Size = 5;
int arr[Size] = {3, 6, 9, 12, 15};
arr[3] = 42;
|
This array holds 5 elements. The size of this array is known at compile time, and it's allocated on the stack.
Then, there are dynamic arrays.
1 2 3 4
|
int size;
size = 142;
int* arr = new int[size];
for (int i = 0; i < size ; i++) { arr[i] = 3 * (i + 1) };
|
The "size" variable here can be any positive number you want. It does not need to be known at compile-time, meaning you can get this value at run-time from a file, or from user input. Both things have the same end result.
Because arr is a pointer to dynamic data, this data can be deleted and new data can be allocated to the pointer.
1 2 3 4 5 6
|
int* arr = new int[5]; // arr now points to a dynamic array of size 5
delete[] arr;
arr = new int[10]; // arr now points to a dynamic array of size 10
delete[] arr;
arr = new int(5); // arr now points to a single int, of value 5.
delete arr;
|
Last (
and least), there are VLAs (Variable-Length Arrays). This is a C
bug feature that, through
non-standard compiler extensions, may be able to compile in C++. It allocates a variable-length array on the stack.
Don't use these in C++. Ever. It's illegal. The C++ standards police will arrest you.
I think it should count as entrapment...
1 2
|
int size = 5;
int arr[size];
|
"size" is
NOT a compile-time constant, and therefore cannot be used as the size of a non-dynamic array in C++.
________________________________________
Now the more important part:
What are the advantages?
The size of a dynamic array can be determined at run time instead of compile time, and the dynamic array can be extended in size. (This is how an std::vector works under the hood -- it's just a thin wrapper to a dynamic array).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <iostream>
int main()
{
int* items = new int[5] {1, 2, 3, 4, 5}; // I have 5 items
for (int i = 0; i < 5; i++)
std::cout << items[i] << std::endl;
// oh, I found a new item. I'm going to add it to my collection.
// I do this by
// (1) allocating a bigger dynamic array
// (2) copying the existing elements from the old array to the new array
// (3) deleting the old array, redirecting its pointer to the new array
int* items_temp = new int[6];
for (int i = 0; i < 5; i++)
items_temp[i] = items[i];
items_temp[5] = 42;
delete[] items;
items = items_temp;
for (int i = 0; i < 6; i++)
std::cout << items[i] << std::endl;
delete[] items;
}
|
Edit: Fixed typos.