Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
void test(){
Dog* pD = new Dog("Gunner");
pD->bark();
//If we return here, or some exceptions happen here
//pD will cause memory leak!
delete pD;
}
//Using unique pointer here
void test01(){
unique_ptr<Dog> pD (new Dog("Gunner"));
pD->bark();
//If we return earlier, or some exceptions happen here
//pD will not cause memory leak!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment