What do I need to do before deleting elements in a vector of pointers to dynamically allocated objects?

后端 未结 5 768
天涯浪人
天涯浪人 2020-11-30 06:35

I have a vector that I fill with pointers to objects. I am trying to learn good memory management, and have a few general questions:

  1. Is it true that when I am
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 07:12

    Everything you allocate with new you have to delete later on. Objects that you don't explicitly allocate with new shouldn't you delete.

    If you don't want to manage the objects manually but want the vector to "own" them, it might be better to store the objects by value instead of storing pointers to them. So instead of std::vector you could use std::vector.

提交回复
热议问题