Of cause I know delete corresponding to new and delete [] corresponding to new []. But sometimes things are not that simple. Imaging you are fixing a bug and need to delete an object somebody else wrote, for example:
1 | std::string *foo = new Foo; |
How will you delete it? Pretty much everyone will write:
1 | delete foo; |
But this is not necessary right if you look a little bit further until you come across this line:
1 | typedef std::string Foo[4]; |
Oops! Foo
is actually array of strings. So you need to use
1 | delete [] foo; |
So what’s the lesson learned here? Probably it’s good for you to comment your typedefs, and as a people editing other’s code, look a little bit further. But other than that, my suggestion is to use STL library instead of dynamically allocate arrays of objects unless it’s absolutely necassary.