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. auto_ptr - passing to functions and best practices

auto_ptr - passing to functions and best practices

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
8 Posts 3 Posters 1 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.
  • C Offline
    C Offline
    Christian Flutcher
    wrote on last edited by
    #1

    void anotherMethod(auto_ptr<Foo> foo)
    {
    cout << "Taken ownership. " << foo->getAString() << endl;
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
    auto_ptr<Foo> foo(new Foo);
    cout << foo->getAString() << endl;
    anotherMethod(foo);
    // this will throw error as the pointer is deleted
    cout << "From main again " << foo->getAString();
    cout << "Ending main" << endl;
    return 0;
    }

    In the avove code, I am trying to pass the auto_ptr to another function. But doing this will make the another function take ownership and the underlying pointer will be deleted when another functions scope ends. So I am unable to access it in the main method again. To workaround this issue, I changed the code like

    void anotherMethod(const auto_ptr<Foo>& foo)
    {
    cout << "Taken ownership. " << foo->getAString() << endl;
    }

    The below code will also work

    void anotherMethod(Foo* foo)
    {
    cout << "Taken ownership. " << foo->getAString() << endl;
    }

    // in main I called like
    auto_ptr<Foo> foo(new Foo);
    anotherMethod(foo.get());

    I am wondering which one is the best approach? Can anyone help? Also is it a good practice to use auto_ptr over normal pointers and is it always guranteed the cleanup?

    C 1 Reply Last reply
    0
    • C Christian Flutcher

      void anotherMethod(auto_ptr<Foo> foo)
      {
      cout << "Taken ownership. " << foo->getAString() << endl;
      }
      int _tmain(int argc, _TCHAR* argv[])
      {
      auto_ptr<Foo> foo(new Foo);
      cout << foo->getAString() << endl;
      anotherMethod(foo);
      // this will throw error as the pointer is deleted
      cout << "From main again " << foo->getAString();
      cout << "Ending main" << endl;
      return 0;
      }

      In the avove code, I am trying to pass the auto_ptr to another function. But doing this will make the another function take ownership and the underlying pointer will be deleted when another functions scope ends. So I am unable to access it in the main method again. To workaround this issue, I changed the code like

      void anotherMethod(const auto_ptr<Foo>& foo)
      {
      cout << "Taken ownership. " << foo->getAString() << endl;
      }

      The below code will also work

      void anotherMethod(Foo* foo)
      {
      cout << "Taken ownership. " << foo->getAString() << endl;
      }

      // in main I called like
      auto_ptr<Foo> foo(new Foo);
      anotherMethod(foo.get());

      I am wondering which one is the best approach? Can anyone help? Also is it a good practice to use auto_ptr over normal pointers and is it always guranteed the cleanup?

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      take a look at boost's shared_ptr. it's reference-counted, so you won't run into problems if you pass one to a function, or store one in an STL container.

      image processing toolkits | batch image processing

      C 1 Reply Last reply
      0
      • C Chris Losinger

        take a look at boost's shared_ptr. it's reference-counted, so you won't run into problems if you pass one to a function, or store one in an STL container.

        image processing toolkits | batch image processing

        C Offline
        C Offline
        Christian Flutcher
        wrote on last edited by
        #3

        Classes from "Boost" looks promising. Thanks for the information. BTW, just for academic interest, can you look into the example I have given in the initial post? I just want to make sure auto_ptr can be used in such a way.

        C M 2 Replies Last reply
        0
        • C Christian Flutcher

          Classes from "Boost" looks promising. Thanks for the information. BTW, just for academic interest, can you look into the example I have given in the initial post? I just want to make sure auto_ptr can be used in such a way.

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #4

          yes, i think your examples are correct. since the problem with auto_ptr, in this situation, is that passing an auto_ptr to a function makes a copy, and the copy takes control of the allocated memory. passing it by reference (either as a ref or as a ptr) avoids that copy, and the outer function maintains control of the memory.

          image processing toolkits | batch image processing

          C 1 Reply Last reply
          0
          • C Chris Losinger

            yes, i think your examples are correct. since the problem with auto_ptr, in this situation, is that passing an auto_ptr to a function makes a copy, and the copy takes control of the allocated memory. passing it by reference (either as a ref or as a ptr) avoids that copy, and the outer function maintains control of the memory.

            image processing toolkits | batch image processing

            C Offline
            C Offline
            Christian Flutcher
            wrote on last edited by
            #5

            Many thanks :)

            1 Reply Last reply
            0
            • C Christian Flutcher

              Classes from "Boost" looks promising. Thanks for the information. BTW, just for academic interest, can you look into the example I have given in the initial post? I just want to make sure auto_ptr can be used in such a way.

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              FYI: Visual C++ 2008 SP1+ has a shared_ptr Class[^] as well. Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              C 1 Reply Last reply
              0
              • M Mark Salsbery

                FYI: Visual C++ 2008 SP1+ has a shared_ptr Class[^] as well. Mark

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                C Offline
                C Offline
                Christian Flutcher
                wrote on last edited by
                #7

                Thanks Mark. But unfortunatly I can't use it as my application is portable across multiple platforms. I believe the one which you said is not portable (please correct me if I am wrong). Your help is much appreciated though. :)

                M 1 Reply Last reply
                0
                • C Christian Flutcher

                  Thanks Mark. But unfortunatly I can't use it as my application is portable across multiple platforms. I believe the one which you said is not portable (please correct me if I am wrong). Your help is much appreciated though. :)

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  Christian Flutcher wrote:

                  portable across multiple platforms

                  In that case Boost is a better option for the time being if you need shared_ptr. shared_ptr will currently only be found on compilers that have implemented the TR1[^] extensions. Mark

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  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