New int[i] array Automatically resizing

Takes a look at this simple code:

int* numbers = new int[1];

for (int i = 0; i < 10; i++)
{
    numbers[i] = i;
}

for (int i = 0; i < 10; i++)
{
    cout << numbers[i] << endl;
}

I provision a new array with 1 element.
I then populate the array for is has 10 elements
Finally a print each element of the array and the result is 0 1 2 3 4 5 6 7 8 9

in the class example its set up so that the input will fail with a cin.fail() check when the array reaches its upper bound. the fail() always returns false.

I don’t understand how the array is resizing itself. I am using c++ 20.

You have mentioned multiple points and displayed only a few.

  1. Declaring a dynamic array numbers with size of 1 int in memory
  1. Adding 10 elements in the array
  1. Printing the result of above operations

You have not mentioned anything about your class or your function using cin.fail().

As for

You have created a dynamic array. It will create extra memory when required. But the code segment you have provided will not work (or should not if it does) as you are accessing and writing to memory outside your array bound.

Kindly provide complete code.

Ok. Here’s the code as Mosh writes it:

int* numbers = new int[5];
int entries = 0;

while(true)
{
    cout << "Number: ";
    cin >> numbers[entries];

    if (cin.fail()) break;
    entries++;
}


for (int i = 0; i < entries; i++)
{
    cout << numbers[i] << endl;
}

delete numbers;
numbers = nullptr;

return 0;

and here is the result copied from the command line:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Number: 6
Number: 6
Number: 7
Number: 8
Number: 10
Number: 11
Number: 12
Number: 13
Number: 14
Number: 15
Number: 16
Number: p
1
2
3
4
5
6
6
7
8
10
11
12
13
14
15
16

The array is dynamically resizing itself.
The only time cin.fail() is returning true is when I enter a non-integer.
Does the array provisioned by new int automatically resize itself? That’s what it is doing.

It is not. I reviewed the video. It cannot check to see if array is empty or not.

Mosh explicitly stated the following:

“So at this point we are not worrying about dynamic memory allocation, you’re just writing a basic program to get a bunch of numbers from the user”

In the next step he included the following part as an exercise and a solution to it:

if(entries == 5)
{
    // Logic to increase the size of array as mentioned in the course
}

Now,

or

The array is not resizing itself. We have used a pointer to play an array. A pointer points to one single address of a memory block (here it is base address of array). Those blocks are randomly selected from free space. There are memory blocks before and after that address.

new operator is used to allocate memory to the variable (if any). Here new int[5] allocated 5 blocks of memory to numbers. As mentioned there are memory blocks before and after the assigned blocks.

You are accessing memory blocks you are not supposed to.

cin.fail() reports the status of last cin operation (there is only one by the way). A fail occurs when memory size do not match, EOF, broken stream or no entered value is still in buffer.

Here, data types do not match. Which is why it returns true.

You need to manually prevent undefined access to memory by resizing the array when required.