0

I am passing a dynamic array to a function with a value that is meant to be added to the array and when I enlarge and reset the array dynamically and iterate over the array I find the last value of the array is a garbage value rather than what is expected. I've looked at a few other posts on SO as well as some documentation and i'm stumped on what i'm doing wrong. I would prefer to use a vector, but my assignment requires a dynamic array unfortunately. Any thoughts? Thanks.

Post above is passing pointers to vectors by reference and has nothing to do with enlarging dynamic arrays

Main

cout << "Please enter the size of the array of integers you would like to create: ";
    cin >> size;

    cout << "\nPlease enter your integers:\n";

    int *integerArray = new int[size];

    //store values in array
    for (int dynamicArrayDataCounter = 0; dynamicArrayDataCounter < size; dynamicArrayDataCounter++)
        cin >> integerArray[dynamicArrayDataCounter];

    cout << "\n Please enter the integer you would like to insert into this array: ";
    cin >> userInt;

    InsertIntegerToSortedList(integerArray, userInt, size);

    //Print array for proof
    for (int counterPrinter = 0; counterPrinter < size + 1; counterPrinter++)
        cout << endl << integerArray[counterPrinter];

    //Remove memory and repoint dangling pointer
    delete [] integerArray;
    integerArray = NULL;

    return 0;
}

Function

void InsertIntegerToSortedList(int *integerArray, int userInteger, int size)
{
    //Declare new array to add index position for integerArray
    int *resizedArray = new int[size + 1];
    bool numInserted = false;

    for (int counter = 0; counter < size + 1; counter++)
    {
        if (integerArray[counter] < userInteger)
        {
            resizedArray[counter] = integerArray[counter];
        }
        else if ((integerArray[counter] > userInteger && integerArray[counter - 1] < userInteger) || integerArray[counter] == userInteger || (integerArray[counter] <= userInteger && size - counter == 1))
        {
            resizedArray[counter] = userInteger;

            numInserted = true;
        }
        else if (numInserted)
            resizedArray[counter] = integerArray[counter - 1];
    }

    //Store resizedArray values in integerArray
    integerArray = resizedArray;

    //Remove dynamic array on heap and repoint dangling pointer
    delete[] resizedArray;
    resizedArray = NULL;
}
StormsEdge
  • 854
  • 2
  • 10
  • 35

1 Answers1

4

In

void InsertIntegerToSortedList(int *integerArray, int userInteger, int size)

you are passing the pointer integerArray by value, hence at the exit of the function you end up not modifying it. Pass it by reference, like

void InsertIntegerToSortedList(int* & integerArray, int userInteger, int size)

Furthermore, as mentioned in the comments, you're doing it slightly wrong. First, copy the array elements into resizedArray. Next, you need to delete the old array,

delete[] integerArray;

and finally assign to the newly allocate array to integerArray

integerArray = resizedArray;

That's all is needed, now integerArray will point to the memory that was allocated via resizedArray. No need to set resizedArray to NULL, it is just a local variable that will cease to exist at the exit of the function. What you care about is just the address of the memory you allocated, and you already have that stored into integerArray pointer.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • I thought arrays were always passed by reference? – StormsEdge Mar 09 '17 at 13:56
  • This is a pointer not an array. – drescherjm Mar 09 '17 at 13:56
  • Also, when I make that correction i'm getting all garbage values when I try and print the array rather than just one garbage value at the end – StormsEdge Mar 09 '17 at 13:57
  • You freed the memory you allocated for the `resizedArray` just before the function exited. You should have freed the memory for the original array instead. – drescherjm Mar 09 '17 at 13:59
  • @drescherjm Yes, but I set integerArray = resized array...am I not allowed to free that memory before hand? – StormsEdge Mar 09 '17 at 14:00
  • and when I remove the delete and null to the resizedArray I get the garbage value at the end again. – StormsEdge Mar 09 '17 at 14:01
  • free the `integerArray` before that assignment. Don't delete the `resizedArray` at all. This is the new buffer you just allocated. Remember that after `integerArray = resizedArray` both pointers point to the same location. – drescherjm Mar 09 '17 at 14:01
  • As for the garbage at the end, the obvious answer is you are not actually inserting the new number at all. Your middle condition is a mess, and I can't figure out what you're trying to check for. There's no guarantee that the numbers entered were sorted in the first place, so the might fail because of that. You're also checking if you already inserted last, when it should be the first thing you check. Try replacing that logic with a simpler "if already inserted, copy, else if the number is smaller, insert it". Outside the loop check if you already inserted it and if not, put it at the end. – Kian Mar 09 '17 at 14:59