steady_progress steady_progress - 18 days ago 5
C++ Question

dynamic memory allocation in C++ without pointers - what's the point of pointers?

Consider the following C++ program:

#include <iostream>
using namespace std;

void printArray(int p_values[], int size, int elements_set);

int main()
int next_element = 0;
int size = 3;
int p_values[size];
int val;
cout << "Please enter a number: ";
cin >> val;

while (val > 0)
if (size == next_element+1)
size *=2;
int p_values[size];
p_values[next_element] = val;
cout << "Current array values are: " << endl;
printArray(p_values, size, next_element);
cout << "Please enter a number (or 0 to exit): ";
cin >> val;


void printArray(int p_values[], int size, int elements_set)
cout << "Total size of array: " << size << endl;
cout << "Number of slots set so far: " << elements_set << endl;
cout << "Values in the array: " << endl;
for (int i = 0; i < elements_set; ++i){
cout << "p_values[" << i << "] = "
<< p_values[i] << endl;

What the code does is simply this:
1. the user is asked for a number
2. the number is saved in an array
3. the current content of the array is printed out

Once the array is full, its size is doubled.
Such a dynamic increase of an array during runtime is usually done with a pointer.
In fact, I was told that such dynamic increasing of an array size is only possible by using pointers.

However, as you can see in the program above, I did not use any pointers but only a static array. Nevertheless, the program dynamically increases the sizeof the array at runtime.

Therefore, my questions are these:
1. Why was I told at university (and also in several books) that, in a C++ program, increasing memory usage during runtime is only possible by using pointers?
2. What's the point of pointers (apart from making it possible to pass variables by reference when calling a function), given the dynamic memory allocation can be done without them?

After having run the program for a while and having entered 8 numbers, the output of the program looks as follows:

Please enter a number (or 0 to exit): 8
Current array values are:
Total size of array: 12
Number of slots set so far: 8
Values in the array:
p_values[0] = 1
p_values[1] = 2
p_values[2] = 3
p_values[3] = 4
p_values[4] = 5
p_values[5] = 6
p_values[6] = 7
p_values[7] = 8
Please enter a number (or 0 to exit):

    size *=2;
    int p_values[size];

Nope. This does not increase the size of the existing p_values array, declared at the beginning of the function. This declares a different array named p_values, in the inner scope...

...and since the inner scope immediately ends here, this has no effect whatsoever. The loop the continues and runs off the end of the original p_values array, resulting in undefined behavior and memory corruption.

Nevertheless, the program dynamically increases the sizeof the array at runtime.

No, it didn't. The size of the existing array was not dynamically increased. A different, larger variable-length array was declared in inner scope, and immediately destroyed. The original array remained at its original size, and undefined behavior resulted.

Just keep inputting more values into the array, and at some point you'll end up smashing the return address on the stack, resulting in a segfault when main() returns (assuming your compiler implements variable-length arrays dynamically by adjusting the size of the current function's stack frame).