-2

I'm writing my own array sorting method and I have created a duplicate array to stored the objects as they are stored. The arrays are both arrays of pointers and so I need to delete my temporary array without deleting the items it points to. In the code snippet below I am leaking memory as I either delete all the items or do not delete anything.

//In constructor initialiser list
m_listArray( new PathFindingTile*[maxSize] )

void sort()
{
    auto** sorted = new PathFindingTile*[m_size];

    sorted[0] = m_listArray[0];

    for (int i = 1; i < m_size; i++)
    {
        sorted[i] = m_listArray[i];
        for (int j = i; j - 1 >= 0; j--)
        {
            if (*m_listArray[j] < *m_listArray[j - 1])
            {
                PathFindingTile* temp = sorted[j - 1];
                sorted[j - 1] = m_listArray[i];
                sorted[j] = temp;
            }
            else
            {
                sorted[i] = m_listArray[i];
                break;
            }
        }
    }

    m_listArray = sorted;

    //Both 'delete sorted' and 'delete[] sorted' delete the contents of sorted, but I'd only like to delete the array pointer
    delete sorted;
}

How can I transfer the duplicated list back to the original list without leaking memory?

Will Anderson
  • 113
  • 1
  • 14
  • You wrote this code. The arrays are of pointers. You have a memory leak or an exception is thrown. I think I follow. What is your programming question? – Drew Dormann Oct 26 '18 at 19:19
  • How do I delete the sorted array without deleting the contents – Will Anderson Oct 26 '18 at 19:20
  • Unrelated: [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) can make this (and the [Rule of Three](https://en.cppreference.com/w/cpp/language/rule_of_three) error you're probably going to run into shortly) go away. – user4581301 Oct 26 '18 at 19:22
  • Please [edit] your question and add a sentence that ends with a question mark. – Drew Dormann Oct 26 '18 at 19:22
  • @DrewDormann that's done – Will Anderson Oct 26 '18 at 19:23
  • @user4581301 I'm unable to use std::vector or std::list due to the parameters of the task I was given, I'm trying to recreate them myself – Will Anderson Oct 26 '18 at 19:24
  • Such is life. Still you owe it to yourself to read through the link on the [Rule of Three](https://en.cppreference.com/w/cpp/language/rule_of_three) if you are not familiar with the concept. It will save much future debugging. – user4581301 Oct 26 '18 at 19:26

2 Answers2

4
 m_listArray = sorted;

On this line, you leak the array that m_listArray was pointing at before the assignment.

You must delete the array before (the only) pointer to it is overwritten. Also, delete is wrong for deleting pointers from new[]. delete[] is correct:

delete[] m_listArray;
m_listArray = sorted;

P.S. It is not a good idea to have bare pointers to owned memory. I recommend using std::vector instead. Also, it should not be necessary to allocate a new array in order to sort one. You could simply swap elements within the array.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Yeah I now see my mistakes, I thought because the pointers in sorted were taken from m_listArray they would simply overwrite the old ones when I copied it back. I've also updated the code to not even use the sorted list now so I guess this is why I shouldn't code at 4am. thanks – Will Anderson Oct 26 '18 at 19:29
0

I think you just remove pointer to the first element in the array. I believe you need to release the array with the follow command. delete[] sorted

Read more about delete and delete[] [delete vs delete[]]1

  • Diagnosed the lesser of two bugs, but the main problem is not `delete[]`ing `m_listArray`. The behaviour of using `delete` where you should have used `delete[]` is undefined. – user4581301 Oct 26 '18 at 20:21