Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. what's the point of auto_ptr & unique_ptr?

what's the point of auto_ptr & unique_ptr?

Scheduled Pinned Locked Moved C / C++ / MFC
iosdata-structuresperformancequestion
6 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Falconapollo
    wrote on last edited by
    #1

    I just can't understand the meaning of unique_ptr or auto_ptr, as we all know, once the parameter gets out of its scope, we won't get memory leaks. However, is it a better idea if I create the parameter on the stack rather on the heap. I don't see its advantage over normal parameters. If we really need to create parameters on the heap, then we must use '

    new

    ' operator, right? Here is a little code snippet:

    class Base
    {
    public:
    Base()
    {
    cout << "Base::()" << endl;
    }
    virtual ~Base()
    {
    cout << "~Base()" << endl;
    }

    virtual void iPhone()
    {
    	cout << "I'm iPhone5s" << endl;
    }
    

    };

    class Derive : public Base
    {
    public:

    Derive()
    {
    	cout << "Derive()" << endl;
    }
    
    virtual void iPhone()
    {
    	cout << "I'm iPhone 6" << endl;
    }
    
    virtual ~Derive()
    {
    	cout << "~Derive()" << endl;
    }
    

    };

    void main()
    {
    {

      auto\_ptr<Base> ptr(new Derive());//a foolish idea to do so?
      ptr->iPhone();
    
      Derive d;//isn't it better?
      Base\* base = &d;
      base->iPhone();
    

    }
    }

    M A C S 4 Replies Last reply
    0
    • F Falconapollo

      I just can't understand the meaning of unique_ptr or auto_ptr, as we all know, once the parameter gets out of its scope, we won't get memory leaks. However, is it a better idea if I create the parameter on the stack rather on the heap. I don't see its advantage over normal parameters. If we really need to create parameters on the heap, then we must use '

      new

      ' operator, right? Here is a little code snippet:

      class Base
      {
      public:
      Base()
      {
      cout << "Base::()" << endl;
      }
      virtual ~Base()
      {
      cout << "~Base()" << endl;
      }

      virtual void iPhone()
      {
      	cout << "I'm iPhone5s" << endl;
      }
      

      };

      class Derive : public Base
      {
      public:

      Derive()
      {
      	cout << "Derive()" << endl;
      }
      
      virtual void iPhone()
      {
      	cout << "I'm iPhone 6" << endl;
      }
      
      virtual ~Derive()
      {
      	cout << "~Derive()" << endl;
      }
      

      };

      void main()
      {
      {

        auto\_ptr<Base> ptr(new Derive());//a foolish idea to do so?
        ptr->iPhone();
      
        Derive d;//isn't it better?
        Base\* base = &d;
        base->iPhone();
      

      }
      }

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      Note: std::auto_ptr has been (mostly) replaced by std::unique_ptr. The use of std::unique_ptr and std::shared_ptr offer better memory management (garbage control, ... ) than naked pointer (google for the difference between unique and shared).

      {
      Derive* p = new Derive;
      // will leak here
      }

      {
      std::unique_ptr p(new Derive);
      // p will be deleted here, no leak.
      }

      In all (most) cases it would be better to not use pointers at all; but when it happens, use std::unique_ptr or stdshared_ptr depending on your need. So in your example, it could simple be :

      {
      Derive d;
      d.iPhone(); // no need to access the Base class.
      }

      In simple examples using one or the other will probably not make a difference, but in larger systems, it will.

      I'd rather be phishing!

      1 Reply Last reply
      0
      • F Falconapollo

        I just can't understand the meaning of unique_ptr or auto_ptr, as we all know, once the parameter gets out of its scope, we won't get memory leaks. However, is it a better idea if I create the parameter on the stack rather on the heap. I don't see its advantage over normal parameters. If we really need to create parameters on the heap, then we must use '

        new

        ' operator, right? Here is a little code snippet:

        class Base
        {
        public:
        Base()
        {
        cout << "Base::()" << endl;
        }
        virtual ~Base()
        {
        cout << "~Base()" << endl;
        }

        virtual void iPhone()
        {
        	cout << "I'm iPhone5s" << endl;
        }
        

        };

        class Derive : public Base
        {
        public:

        Derive()
        {
        	cout << "Derive()" << endl;
        }
        
        virtual void iPhone()
        {
        	cout << "I'm iPhone 6" << endl;
        }
        
        virtual ~Derive()
        {
        	cout << "~Derive()" << endl;
        }
        

        };

        void main()
        {
        {

          auto\_ptr<Base> ptr(new Derive());//a foolish idea to do so?
          ptr->iPhone();
        
          Derive d;//isn't it better?
          Base\* base = &d;
          base->iPhone();
        

        }
        }

        A Offline
        A Offline
        Albert Holguin
        wrote on last edited by
        #3

        It's essentially meant for the cases where you do need the heap... when you're already using new/delete. It eliminates the need to call delete explicitly and prevents leaks by taking care of the deallocation automatically. That's pretty much the crux of it (I personally don't use it at all).

        J 1 Reply Last reply
        0
        • A Albert Holguin

          It's essentially meant for the cases where you do need the heap... when you're already using new/delete. It eliminates the need to call delete explicitly and prevents leaks by taking care of the deallocation automatically. That's pretty much the crux of it (I personally don't use it at all).

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          I never found it especially useful. By the time it arrived I had disciplined myself when I created code that I didn't see failures to deallocate anymore. I did find that attempting to refactor code (others) that did have problems was at best a futile exorcize but maybe I never understood it in detail enough.

          1 Reply Last reply
          0
          • F Falconapollo

            I just can't understand the meaning of unique_ptr or auto_ptr, as we all know, once the parameter gets out of its scope, we won't get memory leaks. However, is it a better idea if I create the parameter on the stack rather on the heap. I don't see its advantage over normal parameters. If we really need to create parameters on the heap, then we must use '

            new

            ' operator, right? Here is a little code snippet:

            class Base
            {
            public:
            Base()
            {
            cout << "Base::()" << endl;
            }
            virtual ~Base()
            {
            cout << "~Base()" << endl;
            }

            virtual void iPhone()
            {
            	cout << "I'm iPhone5s" << endl;
            }
            

            };

            class Derive : public Base
            {
            public:

            Derive()
            {
            	cout << "Derive()" << endl;
            }
            
            virtual void iPhone()
            {
            	cout << "I'm iPhone 6" << endl;
            }
            
            virtual ~Derive()
            {
            	cout << "~Derive()" << endl;
            }
            

            };

            void main()
            {
            {

              auto\_ptr<Base> ptr(new Derive());//a foolish idea to do so?
              ptr->iPhone();
            
              Derive d;//isn't it better?
              Base\* base = &d;
              base->iPhone();
            

            }
            }

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            Read about unique_ptr at "C++11 - the new ISO C++ standard" page[^].

            Veni, vidi, vici.

            1 Reply Last reply
            0
            • F Falconapollo

              I just can't understand the meaning of unique_ptr or auto_ptr, as we all know, once the parameter gets out of its scope, we won't get memory leaks. However, is it a better idea if I create the parameter on the stack rather on the heap. I don't see its advantage over normal parameters. If we really need to create parameters on the heap, then we must use '

              new

              ' operator, right? Here is a little code snippet:

              class Base
              {
              public:
              Base()
              {
              cout << "Base::()" << endl;
              }
              virtual ~Base()
              {
              cout << "~Base()" << endl;
              }

              virtual void iPhone()
              {
              	cout << "I'm iPhone5s" << endl;
              }
              

              };

              class Derive : public Base
              {
              public:

              Derive()
              {
              	cout << "Derive()" << endl;
              }
              
              virtual void iPhone()
              {
              	cout << "I'm iPhone 6" << endl;
              }
              
              virtual ~Derive()
              {
              	cout << "~Derive()" << endl;
              }
              

              };

              void main()
              {
              {

                auto\_ptr<Base> ptr(new Derive());//a foolish idea to do so?
                ptr->iPhone();
              
                Derive d;//isn't it better?
                Base\* base = &d;
                base->iPhone();
              

              }
              }

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #6

              In general it may indeed be better to create it on the stack. The reason you'd use new is when you want more control over it's lifetime. For example you can't use the stack if it need to stay "alive" after the function returns (more generally, when it goes out of scope).

              Steve

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups