delete[] an array of objects

后端 未结 5 1212
孤街浪徒
孤街浪徒 2020-11-29 04:12

I have allocated and array of Objects

Objects *array = new Objects[N];

How should I delete this array? Just

delete[] array         


        
5条回答
  •  时光取名叫无心
    2020-11-29 04:27

    As a general rule you should delete/delete[] exactly those things that you allocated with new/new[]. In this case you have one allocation with new[], so you should use one call to delete[] to free that allocated thing again.

    That the deletes in the for-loop won't compile is also a good indication that they are not the right way to do it.

提交回复
热议问题