Should I always call vector clear() at the end of the function?

后端 未结 5 693
灰色年华
灰色年华 2020-12-08 14:34

I have some simple function that uses vector like this (pseudo code):

void someFunc(void) {

    std::vector contentVector;

    // here a         


        
5条回答
  •  攒了一身酷
    2020-12-08 14:38

    Since I don't think anyone else has mentioned this, but if your vector was

    std::vector vector;
    

    you should free the memory allocated to each element before the function finishes (unless you've passed ownership to somewhere else - such as a global variable etc)

    for (auto i : vector) {
      delete i;
    }
    

提交回复
热议问题