0

I'm making a function in C++ that takes an array and the size of the array as the parameters. The function is supposed to create a new array twice the size of the received one. Then it should copy the elements from the first array into the new one and fill the remaining slots in the array with 0.

int main()
{
    // get number between 0 and 50 from user, n
    // for (i = 0; i < n; i++):
    //     append random number (0 - 1000) to dataList array

    int * newList = myFunction(dataList, n, z);

    // NEWLIST TEST
    cout << "newlist address: " << newList << endl;
    for (int i = 0; i < n * 2; i++)
    {
        cout << newList[i] << " ";
    }
    cout << endl;
    ///////////////

    for (int i = 0; i < n * 2; i++)
    {
        cout << newList[i] << endl;
    }
}

int * myFunction(int x[], int y)
{
    int z[y * 2];
    for (int i = 0; i < y; i++)
    {
        z[i] = x[i];
    }
    for (int i = 0; i < y; i++)
    {
        z[i + y] = 0;
    }

    // Z TEST
    cout << "z address: " << z << endl;
    for (int i = 0; i < y * 2; i++)
    {
        cout << z[i] << " ";
    }
    cout << endl;
    ///////////

    return z;
}

I output the addresses of newlist (in main) and z (in myFunction) to test if the addresses are the same, and they are.

I also output the contents of the address while in myFunction and while in main. The contents of Z are correct, 4 random numbers and 4 zeroes. However, the contents of newList are always different and unpredictable, which leads me to believe the memory for the address of z is cleared once the function ends.

How do I make this where the contents of the address of newList are kept as they are before the function ends?

  • Try `int* z = new int[y * 2];` instead of `int z[y * 2];`. You are making your Z array with automatic storage which means it is destroyed after the function returns. The *pointer value* of Z is returned from the function but it is pointing to an array that no longer exists. Using the `new` keyword prevents automatic storage and should be paired with an eventual `delete[]` to free the array when it is no longer needed. – Romen Oct 15 '19 at 20:50
  • @Romen This works, thank you. – Trevor Kerney Oct 15 '19 at 21:05
  • 1
    As an alternative to `new[]` and `delete[]`, may I suggest [`std::vector`](https://en.cppreference.com/w/cpp/container/vector)? If `vector` is disallowed, look into [`std::unique_ptr`](https://en.cppreference.com/w/cpp/memory/unique_ptr) and `std::make_unique`. In general [you should avoid `new`](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – user4581301 Oct 15 '19 at 21:06
  • 1
    Alternatively you can make `static int z[y*2];` this means the storage is persisted between function calls. Thus you can return a pointer (though I would recommend you return something else like a reference). If you want to dynamically create the memory as suggested by @Romen then return `std::auto_ptr` from the function. Or even better a `std::vectir as sugggested by user458. – Martin York Oct 15 '19 at 21:07
  • @user4581301, I agree that there are better ways to pass around arrays than with `new` and pointers. But `new` and `delete` are keywords in the language and there is a time and place to use them. I wouldn't shy away from ever using them, or else you never learn their semantics and *how* memory leaks are caused. – Romen Oct 15 '19 at 21:29
  • I stand by my statement. `new` and `delete` are part of the language, but so is `goto`. All three are not to be feared, but should only be used when there is a clear advantage. That's rare. Really rare in the case of `goto`. Learning is a completely different beast, but if all a programmer is taught is `new`/`delete`, they have a lot of unlearning to do to maximize their usefulness outside of school. – user4581301 Oct 15 '19 at 21:47

0 Answers0