Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Last active July 13, 2019 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/edf3b18710334baeed02dd4707202158 to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/edf3b18710334baeed02dd4707202158 to your computer and use it in GitHub Desktop.
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