Dynamic Array vs Array Benefits

closed account (NCRLwA7f)
After looking into dynamic arrays I found this on cplusplus http://www.cplusplus.com/forum/beginner/7961/#msg36866
So if someone can please clarify to me the advantages of using pointers for a dynamic array vs using just a plain array, that would be great. (advantages besides the fact that a dynamic array can be deleted once it's not needed anymore) This of course also beyond the uses of better alternatives such as vectors or more advanced methods.

I have been doing arrays for a short time and this is how I code for an array, it might not be the most effective or resourceful, but it has worked for my needs so far.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>
using namespace std;

//----PROTOTYPE------//
int COUNT_DATA();
void FILL_ARRAY(int array[], int size);

     ifstream inFile;
int main()
{

  int maxSize = 0;
		
  maxSize = COUNT_DATA();
		
     inFile.open("example.txt");
	
  int Array[maxSize];
		
  FILL_ARRAY(Array, maxSize);

  for(int i = 0; i < maxSize; i++)
     cout << Array[i] << endl;

		
    inFile.close();
    return 0;
}

//----FUNCTIONS----//
int COUNT_DATA()
{
	inFile.open("example.txt");
	int cnt = 0, value = 0;
		
	while(inFile >> value)
		cnt++;
		
	inFile.close();
	return cnt;
}

void FILL_ARRAY(int array[], int size)
{
	for(int i = 0; i < size; i++)
	   inFile >> array[i];
}


in the link provided above, helios states that an array of unknown size is not really possible. The user would then have to enter the size needed for that particular instance.

1
2
3
4
5
6
7
8
9
10
11
12

int main()
{
  inNumb.open("example.txt");
		
	int *pointAry;
	int size = 0;
      
        cin >> size;

	pointAry = new int[size];


How is this any better than the 1st example of code, besides that, you don't have to count data and that it is more verbose.
The way I see it, you still have to declare a size of the array whether it's temporary or permanent.
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.
Last edited on
How is this any better than the 1st example of code,

the first example is illegal.

1
2
3
4
5
6
7
  int maxSize = 0;
		
  maxSize = COUNT_DATA();
		
     inFile.open("example.txt");
	
  int Array[maxSize];


here maxSize has to be const
eg

1
2
3
4
5
6
7
  const int maxSize = 0;
		
  maxSize = COUNT_DATA();
		
     inFile.open("example.txt");
	
  int Array[maxSize];



but my compiler allows ...
don't care its illegal


one reason to use a dynamic array over a static array is stack overflow. There is less stack
memory available than dynamic memory. so if you use stack memory you run a higher risk of running out of memory.

Another reason to use a dynamic array is because the size of the array can be determined while the program is running.
closed account (NCRLwA7f)
Wow, these are all really good points. I wish my professor would have told me about some of these illegal issues.

So let’s say you allocate 100 “blocks” of memory with myAr = new int [100];
But only use 80, the other 20 are not “technically “ used?

Thank you for the response
Sure, that's perfectly legal. You can always allocate more space than needed. You just can't allocate less space than needed, or you're gonna have a bad time.
Topic archived. No new replies allowed.