Kaune
Kaune

Reputation: 73

Returning an array address from a function to a pointer (C++)

My homework assignment is quoted below. In case I misread something.

"In a file called PointerPractice.cpp, write a function called concatArrays that takes two int arrays as arguments and concatenates the two arrays into a third larger array that the function creates. The function should use only pointer arithmetic (no array notation) and should return a pointer to the newly created array.  The function should have the following prototype:    int* concatArray(int* pArray1, int size1, int* pArray2, int size2).

In your main function:

  1. Create two int arrays of size 8 and 10.
  2. Using loops, fill both arrays with random numbers.  Each random number should be in the range 0 to 9 (inclusive).
  3. Print both arrays with some annotation indicating which array is being printed.
  4. Now call concatArray with the two random arrays and their sizes as arguments.
  5. Using a pointer, save a reference to the returned array from concatArray.
  6. Finally, print the returned array with some annotation indicating that it’s the resulting array from concatArray."

When I print from the function itself, it prints out correctly. When I try to print it out in main, it prints out random strings of numbers per address, not a single number from 0-9.

The function:

int* concatArray(int* pArray1, int size1, int* pArray2, int size2)
{
    int newArray[size1+size2];

    for(int i=0; i<size1+size2; i++)
    {
        if(i>size1)
        {
            *(newArray+i) = *(pArray2+i-size1);
        }

        else
        {
            *(newArray+i) = *(pArray1+i);
        }
    }

    cout<<endl;

    for(int i=0; i<size1+size2; i++)
    {
        cout << *(newArray+i)<<endl;
    }

    return newArray;

Where I called function in main:

ptrConcatArray = concatArray(myArray1, size1, myArray2, size2);

    cout <<endl;
    cout << "Resultant array:" << endl;

    for(int i=0; i<size1; i++)
    {
        cout << *(ptrConcatArray+i)<<endl;
    }

Upvotes: 2

Views: 1074

Answers (2)

michaeltang
michaeltang

Reputation: 2898

int newArray[size1+size2]; is local variable, it can not be a return value.

int newArray = new int[size1+size2]; should be ok.

Upvotes: 0

Mike Seymour
Mike Seymour

Reputation: 254431

You're returning a pointer to a local array. The array has been destroyed by the time the function returns, leaving the pointer dangling (that is, not pointing to anything valid).

You should return std::vector<int>, a dynamic array which is correctly copyable (unlike the built-in array types), and manages the array's memory automatically.

If your assignment forbids using the standard library (as it seems to when it says "using a pointer"), then perhaps you could return a pointer to an array allocated with new:

int * newArray = new int[size1+size2];

This is a bad idea, since it places the onus on the caller to delete the array once it's finished with; but the worst that can happen is a memory leak, not the memory corruption and undefined behaviour you'd get by returning a dangling pointer.

Upvotes: 2

Related Questions