Last active
July 13, 2019 13:30
-
-
Save zhangxiaomu01/edf3b18710334baeed02dd4707202158 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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