Pointers - getting unexpected output

I am following Mosh C++ tutorial and tried the following.
The output in the parseString function is fine but the output in the main function is not.
What am I doing wrong, please?

void parseString(string &input, int numbers[], int &capacity)
{
    stringstream stream;
    stream.str(input);

    int index = 0;

    while (!stream.eof())
    {
        if (index == capacity)
        {
            capacity++;
            int *temp = new int[capacity];
            for (int i = 0; i < index; i++)
                temp[i] = numbers[i];
            delete[] numbers;
            numbers = temp;
            temp = nullptr;
        }

        int number = 0;
        stream >> number;
        numbers[index] = number;
        cout << index << ": " << numbers[index] << endl; // Here I get the expected results

        index++;
    }
}

void main()
{
    string str = "10 20 30 40 50 60";
    int capacity = 1;
    int *numbers = new int[capacity];

    parseString(str, numbers, capacity);

    for (int i = 0; i < capacity; i++)
        cout << i << ": " << &numbers[i] << " - " << numbers[i] << endl; 
       // Here I DO NOT get the expected results

    numbers = nullptr;
}

Output:
0: 10
1: 20
2: 30
3: 40
4: 50
5: 60
0: 00912DB0 - 9514736
1: 00912DB4 - 9514688
2: 00912DB8 - 50397186
3: 00912DBC - 134261297
4: 00912DC0 - 9515496
5: 00912DC4 - 9509888

My C++ is a bit rusty. But I can see that you are passing numbers to the function, then deleting it and recreating it over and over again. What you end up with when you are done is an array of 1.

If you rewrite your tests in the function to have a loop to print it, you will see it’s broken there.

My guess is the delete logic should not be inside the loop. But I really don’t understand what the code is supposed to do … as I said, I am rusty with C++ … but I can see enough to say the error is in the function, not in main().

------ I continued working on this ------ Here is the corrected code.

#include <sstream>
#include <iostream>

using std::cout, std::endl, std::string, std::stringstream;

void parseString(string &input, int *&numbers, int &capacity) {
    stringstream stream;
    stream.str(input);

    int index = 0;

    while (!stream.eof()) {
        if (index == capacity) {
            capacity++;
            int *temp = new int[capacity];
            for (int i = 0; i < index; i++)
                temp[i] = numbers[i];
            delete[] numbers;
            numbers = temp;
        }

        int number = 0;
        stream >> number;
        numbers[index] = number;
        cout << index << ": " << numbers[index] << endl;

        index++;
    }
}

int main() {
    string str = "10 20 30 40 50 60";
    int capacity = 1;
    int *numbers = new int[capacity];

    parseString(str, numbers, capacity);

    for (int i = 0; i < capacity; i++)
        cout << i << ": " << &numbers[i] << " - " << numbers[i] << endl;
}

Jerry

In your formal parameters, you are assuming that your array will work as a reference or as a pointer (as generally demonstrated using swapping of values). It would not.

Pointers and references are different.
Pointers and arrays are different.

@JerryHobby has used reference to a pointer.

You can also achieve the same result using a pointer to pointer (tedious):

#include <iostream>
#include <sstream>

using namespace std;

void parseString(string &input, int **numbers, int &capacity)
{
    stringstream stream;
    stream.str(input);

    int index = 0;
    
    cout << "Parsing:\n";

    while (!stream.eof())
    {
        if (index == capacity)
        {
            capacity++;
            int *temp = new int[capacity];
            for (int i = 0; i < index; i++)
                temp[i] = (*numbers)[i];
            
            delete[] *numbers;
            // delete[] numbers[0];    // Can also be used
            *numbers = temp;
            temp = nullptr;    // Does not matter as temp is deleted
                               // from stack (out of scope) in each iteration
                               // and is redefined in the next one
        }

        int number = 0;
        stream >> number;
        (*numbers)[index] = number;
        // numbers[0][index] = number;    // Can also be used
        
        cout << index << ": " << (*numbers)[index] << endl;
        
        index++;
    }
}

int main()
{
    string str = "10 20 30 40 50 60";
    int capacity = 1;
    int *numbers = new int[capacity];

    cout << "Before parsing:\n";
    
    for (int i = 0; i < capacity; i++)
        cout << i << ": " << &numbers[i] << " - " << numbers[i] << endl;
    
    cout << '\n';

    parseString(str, &numbers, capacity);
    
    cout << "\nAfter parsing:\n";

    for (int i = 0; i < capacity; i++)
        cout << i << ": " << &numbers[i] << " - " << numbers[i] << endl;
    
    numbers = nullptr;
    
    return 0;
}

Deleting a pointer.
Lifetime of objects.