Reputation: 27
I wrote a program as follows:
if(i = 0)
{ vector<int> data;
vector<int>::iterator it;
data.push_back(2);
data.push_back(3);
sort(data.begin(),data.end());
it = data.begin();
}
if(i = 0)
{ vector<int> data;
vector<int>::iterator it;
data.push_back(5);
data.push_back(1);
sort(data.begin(),data.end());
it = data.begin();
}
If I use the vector<int>
two times, will it release automatically? Should I release the memory?
Upvotes: 0
Views: 58
Reputation: 1255
Memory allocation for local variables is deleted automatically when the variable goes out of scope.
if(i == 0)
{
std::vector<int> data; //local variable
}
//allocation for 'data' is deleted automatically
if(i == 0)
{
std::vector<int> data; //this is not the same vector as above
//in fact, the vector above no longer exists at this point
}
//allocation for 'data' is deleted automatically
Upvotes: 4