Click here to Skip to main content
15,907,392 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

vector<unsigned int>* pvecnDigit = new vector<unsigned int>;

pvecnDigit->push_back(2);

pvecnDigit = NULL;


Here, I did not use delete to kill vector pointer but set it to NULL. Is there any Memory Leak?
Posted

Yes. The object created by new still exist, but can't be accessed.
 
Share this answer
 
Yes, you need to delete the vector pointed to by pvecnDigit:
vector<unsigned int="">* pvecnDigit = new vector<unsigned int="">;
pvecnDigit->push_back(2);
delete pvecnDigit;
pvecnDigit = NULL;


Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Volynsky Alex 10-Aug-12 9:55am    
Good answer!
Espen Harlinn 10-Aug-12 9:57am    
Thank you :-D
Sergey Alexandrovich Kryukov 10-Aug-12 15:18pm    
Of course, a 5.
--SA
Espen Harlinn 10-Aug-12 15:35pm    
Thank you, Sergey :-D
I wonder why you even bothered to ask, if you already knew about using delete. If your reason for asking is that you want to know how to avoid having to take care of memory allocation and deallocation, just put that vector on the stack, rather than creating it on the heap:
C++
{
  vector<unsigned int> vecnDigit;
  vecnDigit.push_back(2);
}

Note that instead of creating the vector on the heap, I just put the declaration and use of the vector into a code block, limiting it's scope. That way your use of that vector is still limited to the lines of code between your declaration, and the end of this block. But as it's declared on the stack, you don't need to take care of allocation or deallocation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900