0
ppTile = new Tile*[tileN];
    for(int x=0; x<tileN; x++)
    {
        ppTile[x] = new Tile(Tile::TileType(pCData->GetdefaultTile()),
                            ((x*2)+1) % (mapSize+(mapSize-1)),
                            ( x/ ((float)mapSize-0.5) )+1,
                            pCData->GetdefaultHeight()
                            );
    }

How will I delete both the array of Tile* and the Tile objects?

Edit: This is my guess:

Map::~Map()
{
    if(ppTile)
    {
        for(int x=0; x<mapSize*(mapSize-1); x++)
        {
            delete ppTile[x];
        }
        delete[] ppTile;
    }
}

Is that right?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mickael Bergeron Néron
  • 1,472
  • 1
  • 18
  • 31
  • 3
    Do you actually need pointers to `Tile`s? It sounds like a `std::vector` would probably work. – chris Apr 04 '13 at 21:46
  • `delete` all the `Tile` objects, one by one, then `delete[]` the array. – jrok Apr 04 '13 at 21:47
  • Oh, and make sure you're aware of the Rule of Three/Five. It will bite you if you're not and you choose not to use RAII. – chris Apr 04 '13 at 21:56
  • What is the rule of 3/5? If it has to do with dividing integers, I am intentionally doing so for the rounding downs. – Mickael Bergeron Néron Apr 04 '13 at 21:58
  • @MickaelBergeronNéron no, it's about copying/assigning objects of that type and ending up with nasty situations that arise from the default shallow copying. Again, you can find good resources here on [so]. – Luchian Grigore Apr 04 '13 at 21:59
  • [Relevant](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three), [relevant in C++11](http://stackoverflow.com/questions/4782757/rule-of-three-becomes-rule-of-five-with-c11), [MOST RELEVANT - READ THIS](http://klmr.me/slides/modern-cpp/#1). – chris Apr 04 '13 at 22:02

1 Answers1

4

You iterate through the Tile*s and delete each one, and then you delete[] the big one.

for(int x=0; x<tileN; x++)
{
    delete ppTile[x];
}
delete[] ppTile;

A good rule of thumb is to have a delete for each new and a delete[] for each new[].

You could avoid the hassle by just using std::vector of smart pointers.

As per your edit:

I would avoid the check if(ppTile). Calling delete on a NULL pointer is well-defined, but the check is also prone to errors - if that pointer is NULL and mapSize*(mapSize-1) isn't 0, you'd be hiding a logical error.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Shouldn't it be `delete[] ppTile` or am I mistaken? – Jengerer Apr 04 '13 at 21:49
  • While I don't find this too much of an hassle, would that still be preferable to use std::vector? If so, why? Thanks by the way. – Mickael Bergeron Néron Apr 04 '13 at 21:49
  • @MickaelBergeronNéron numerous posts on why you should prefer containers. Won't reiterate the reasons here - http://stackoverflow.com/questions/381621/using-arrays-or-stdvectors-in-c-whats-the-performance-gap – Luchian Grigore Apr 04 '13 at 21:52
  • @MickaelBergeronNéron, Look up RAII. It's the key term for vectors, smart pointers, and everything. – chris Apr 04 '13 at 21:57