Skip to content

Instantly share code, notes, and snippets.

View zhangxiaomu01's full-sized avatar
🎯
Focusing

Jun Zhang zhangxiaomu01

🎯
Focusing
View GitHub Profile
//using customized deleter
shared_ptr<Dog> pSD(new Dog[3], [](Dog *p){delete []p;});
//for unique pointer, we do not need customized deleter.
//Need to indicate it in template parameter: unique_ptr<Dog[]>.
unique_ptr<Dog[]> dogs(new Dog[3]);
void foo(unique_ptr<Dog> p){
p->bark();
}
unique_ptr<Dog> getDog(){
unique_ptr<Dog> p(new Dog("Jack"));
//return p will use the move semantics!
//so p will no longer has the ownership of Jack
return p;
}
void test02(){
//unique pointer can access the same object at different time
unique_ptr<Dog> pD (new Dog("Gunner"));
unique_ptr<Dog> pD1 (new Dog("Smile"));
pD1->bark();
//Smile will be destroyed, and pD1 owns pD now
pD1 = move(pD);
pD1->bark();
//release() function will return the raw pointer. It will also
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"));
#include<iostream>
#include<string>
#include<memory>
using namespace std;
class Dog {
weak_ptr<Dog> m_pFriend;
public:
string m_name;
Dog(string name) : m_name(name) { cout << "Dog: " << m_name << " is defined!" << endl; }
class Dog {
shared_ptr<Dog> m_pFriend;//cyclic reference
//weak_ptr<Dog> m_pFriend; //Solution to fix the problem
public:
string m_name;
Dog(string name) : m_name(name){cout << "Dog: " << m_name << " is defined!" << endl;}
void bark() {cout << "Dog " << m_name << " rules!" << endl;}
~Dog() {cout << "Dog is destroyed: " << m_name << endl;};
void makeFriend(shared_ptr<Dog> f){m_pFriend = f;}
};
void booFunc(){
//Another way to create a shared pointer:
//Faster and safer/ Exception safe
shared_ptr<Dog> p = make_shared<Dog>("Ink");
shared_ptr<Dog> p1 = make_shared<Dog>("Gunner");
shared_ptr<Dog> p2 = make_shared<Dog>("Tank");
// In the following situation, Gunner is deleted
p1 = p2;
//p1 = nullptr;
void foo_01Func(){
Dog* d = new Dog("Tank"); //Should not use
shared_ptr<Dog> p(d); // p.get_count() == 1
//Here when p goes out of scope, d will be destroyed.
//Then p2 goes out of scope, p2 will be destroyed again...
shared_ptr<Dog> p2(d); // p2.get_count() == 1, the counter won't increase
/* Lesson: An object should be assigned to a shrared_pointer
immediately when it is created. The above case does not follow
this rule. We first create the raw pointer d and then initialize
void fooFunc(){
//We will have a count to keep track of how many pointers are pointing to the object.
shared_ptr<Dog> p(new Dog("Gunner")); // count == 1 now
//This is not allowed
//shared_ptr<Dog> pt = new Dog("Smile");
//Returns the raw pointer.
//In general, avoid using raw pointer when use smart pointers
Dog* ptr = p.get();
//Hard to track when to delete pointer in large project
//We need to free the memory at the right step!
void foo(){
Dog* p = new Dog("Gunner");
//...
delete p;
//...
p->bark(); // p is a dangling pointer now -- undefined behavior
}// If we do not delete p, then we will have memory leak