-1

I have the following code:

class MyList
{
    private:

    public:
        int* list;
        int size = 0;
        int max;

        // constructor
        MyList(int s)
        {
            max = s;
            size = 0;
            if(max > 0)
                list = new int[max];
        };

        // destructor
        ~MyList()
        {
            for (int x = 0; x < max; x++)
                delete (list + x);
        };
};

I tried to clear the memory with that destructor. However, it throws an error on second iteration. What did I do wrong? Also, it wouldn't let me do it this way:

delete list[x];

Can someone explain to me why? Thank you so much.

CaTx
  • 1,421
  • 4
  • 21
  • 42
  • What error does it throw? – Robert Columbia Jan 24 '18 at 05:36
  • This is what VC++ says: TheBasics.exe has triggered a breakpoint. occurred. – CaTx Jan 24 '18 at 05:41
  • 3
    @CaTx - In the last three hours you have posted 3 questions about C++. Would it be better to read a text book on the subject? – Ed Heal Jan 24 '18 at 05:44
  • Ah. This makes the last question clear. You have been tasked with re-implementing `vector`. You should most definitely read [What is The Rule of Three?](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). It will save you a great deal of debugging in the near future. – user4581301 Jan 24 '18 at 05:52
  • `int* list;` is one pointer. There is no need for a loop to deallocate it more than once. – user4581301 Jan 24 '18 at 05:54
  • Professor never mentioned this Rule of Three. It's the first week of class. – CaTx Jan 24 '18 at 05:54
  • Not all professors do, unfortunately. Last week I interviewed a potential hire with a masters degree. When asked "What is the Rule of three?" he tried to invent something magical. That said, writing your own `vector` is not stuff you should be learning in the first week of C++. You aren't coming into programming fresh, so may I recommend supplementing your course materials with the [C++ Primer](https://www.amazon.com/Primer-5th-Stanley-B-Lippman/dp/0321714113/)? It takes a bit of time getting you up to speed on the syntax and ideologies before throwing you off the deep end. – user4581301 Jan 24 '18 at 06:09
  • Thanks, mister. It's good to pick up stuffs along the way. – CaTx Jan 24 '18 at 06:11

3 Answers3

6

You should use delete[] because list is created via new[]-expression. e.g.

// destructor
~MyList()
{
    delete[] list;
}

Note that they must be pair; new int[max] create an array containing max's elements, delete[] destroy the whole array. delete should only be used for pointer created by new.

And better to change the constructor to

// constructor
MyList(int s)
{
    max = s;
    size = 0;
    if(max > 0)
        list = new int[max];
    else
        list = nullptr;
}

to make sure list is always valid.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • Can you explain to me how 'delete[] list;' works? Does it go through all assigned elements? I never see it before. Thanks. – CaTx Jan 24 '18 at 05:40
  • @CaTx They must be pair; `new int[max]` create an array with `max`'s elements, `delete[]` destroy the whole array. `delete` should only be used for pointer created by `new`. – songyuanyao Jan 24 '18 at 05:42
  • Thanks. I changed it into this: int* list = nullptr; – CaTx Jan 24 '18 at 05:46
2

Try this:

MyList(int s)
: max(s),
  size(0),
  list(new int[s])
{
};

~MyList()
{
    delete[] list;
};
Sid S
  • 6,037
  • 2
  • 18
  • 24
0

i dnt understand why are you using a loop to deallocate that memory.... you should simpy write

delete[] list;

that would be enough! in your destructor you are using delete (list(a pointer)+x) this is not deallocating memory you created... you are tryin to delete addresses next to your list by adding value of x loop in it i hope you understood your error :)

Warda Baig
  • 36
  • 1
  • 2
  • I previously used linked list for another class, so that way of thinking carried over. The people here have enlightened me with new info tho there has been no explanation so far about the underlying mechanism of 'delete[] list;'. – CaTx Jan 24 '18 at 07:06
  • have a look at this code int *pi[4],i,j,row,col; cout << "Enter row and col:"; cin >> row >> col; pi[4] = new int[row]; // by writing this i simply created a few locations dynamically for (i = 0; i < row; i++){ pi[i] = new int[col]; //but by writing this i created locations corresponding to every location i created earlier seprately } } – Warda Baig Jan 24 '18 at 07:29
  • and to deallocate the memory i created in loop } for (i = 0; i < row; i++){ delete[]pi[4]; } thats how it will be done – Warda Baig Jan 24 '18 at 07:30
  • in your case delete [] list you created a few locations that are being deallocated ..no need for a loop – Warda Baig Jan 24 '18 at 07:32