Array of structs and new / delete

后端 未结 8 899
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 18:47

I have a struct like this:

class Items 
{
private:
    struct item
    {
        unsigned int a, b, c;
    };
    item* items[MAX_ITEMS];
}

8条回答
  •  攒了一身酷
    2021-01-02 19:33

    Setting items[5] to NULL doesn't delete the memory associated with the item, it simply sets the pointer to that item to NULL, therefore the memory is leaked.

    You can delete the item by calling:

    delete items[5];
    

    Since C++ has not automatic garbage collection, you need to delete any memory you no longer need.

提交回复
热议问题