[C + +] 37 μ intelligent pointer analysis

Keywords: C++ Programming

Eternal topic

  • Memory leak (infamous Bug)

    • Dynamic request heap space, do not return after use
    • There is no garbage collection mechanism in C + + language
    • Pointer cannot control the life cycle of the indicated heap space

Programming experiments: memory leaks

#include <iostream>
#include <string>

using namespace std;

class Test
{
private:
    int i;
public:
    Test(int i)
    {
        this->i = i;
    }
    int value()
    {
        return i;
    }
    ~Test()
    {
    }
};

int main()
{
    for(int i=0; i<5; i++)          // What about 5000000?
    {
        Test* p = new Test(i);
        
        cout << p->value() << endl;
    }

    return 0;
}
Output:
0
1
2
3
4

Deep thinking

  • What do we need

    • Need a special pointer
    • Actively freeing heap space at the end of the pointer life cycle
    • A heap space can only be represented by at most one pointer (to avoid memory multiple releases)
    • Prevent pointer operation and pointer comparison (to avoid wild pointer caused by crossing boundary)

Intelligent pointer analysis

  • Solution

    • Overloaded pointer characteristic operators (- > and *)
    • Can only be overloaded by a class's member function
    • Overloaded functions cannot use parameters (only one overloaded function can be defined)

Programming experiment: intelligent pointer

#include <iostream>
#include <string>

using namespace std;

class Test
{
private:
    int i;
public:
    Test(int i)
    {
        cout << "Test(int i)" << endl;
        this->i = i;
    }
    int value()
    {
        return i;
    }
    ~Test()
    {
        cout << "~Test()" << endl;
    }
};

class Poniter
{
private:
    Test* m_pointer;
public:
    Poniter(Test* p = NULL)
    {
        m_pointer = p;
    }
    Poniter(const Poniter& obj)
    {
        m_pointer = obj.m_pointer;                    // Transfer of ownership
        const_cast<Poniter&>(obj).m_pointer = NULL;
    }
    Poniter& operator = (const Poniter& obj)
    {    
        if( this != &obj )
        {
            delete m_pointer;                         // Transfer of ownership
            m_pointer = obj.m_pointer;
            const_cast<Poniter&>(obj).m_pointer = NULL;        
        }
        
        return *this;
    }
    Test* operator -> ()
    {
        return m_pointer;
    }
    Test& operator * ()
    {
        return *m_pointer;
    }
    bool isNull()
    {
        return (m_pointer == NULL);
    }
    ~Poniter()
    {
        delete m_pointer; 
    }
};

int main()
{
    Poniter p1 = new Test(0);
    
    cout << p1->value() << endl;
    
    Poniter p2 = p1;
    
    cout << p1.isNull() << endl;
    cout << p2->value() << endl;

    return 0;
}
Output:
Test(int i)
0
1
0
~Test()

  • Rules for the use of smart pointers: they can only be used to point to objects or variables in the heap space

Summary

  • Pointer characteristic operators (- > and *) can be overloaded
  • Overloaded pointer signatures can use objects instead of pointers
  • Smart pointers can only be used to point to memory in heap space
  • The significance of intelligent pointer lies in avoiding memory problems of maximum program

For the above content, please refer to the series of courses of Ditai Software Institute, and protect the original

Posted by kevinc on Tue, 10 Dec 2019 05:29:02 -0800