0
std::vector<int*> * vec=new std::vector<int*>();
int* p=new int();
int* q=new int();
vec.push_back(p);
vec.push_back(q);
.
.
.
vec.clear();

My Question is will the memory is deallocated ???

2
  • Correct me if I'm wrong, but vector::push_back takes an argument of type T and not void? Commented Mar 5, 2013 at 6:27
  • @H2CO3 Yes that is correct, but it is not particularly relevant to the question. Commented Mar 5, 2013 at 6:29

2 Answers 2

3

The memory holding the pointers, yes. The memory holding pointer's contents, no. Also, you should free the vector itself by writing delete vec; at the end of the program, but I guess, that it's not what you are asking for.

Sign up to request clarification or add additional context in comments.

Comments

2

No. You should use delete, when you use new (and not use smart-pointers). And for elements of vector - no, the memory will not be deallocated, you should use delete on each element in vector. Something like

std::for_each(vec.begin(), vec.end(), [](const int* p) { delete p; });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.