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. how is boost_scoped_ptr RAII ?

how is boost_scoped_ptr RAII ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 3 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.
  • E Offline
    E Offline
    elelont2
    wrote on last edited by
    #1

    I can do the following:

    boost::scoped_ptr class;

    class->DoSomething(); // Has not been initialized yet

    class.reset(new MyClass);

    Doesn't it go against RAII? I keep reading that scoped_ptr is RAII but i can use object before it is initialized. Thanks

    C A 2 Replies Last reply
    0
    • E elelont2

      I can do the following:

      boost::scoped_ptr class;

      class->DoSomething(); // Has not been initialized yet

      class.reset(new MyClass);

      Doesn't it go against RAII? I keep reading that scoped_ptr is RAII but i can use object before it is initialized. Thanks

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

      and what happens when you run that?

      image processing toolkits | batch image processing

      A E 2 Replies Last reply
      0
      • E elelont2

        I can do the following:

        boost::scoped_ptr class;

        class->DoSomething(); // Has not been initialized yet

        class.reset(new MyClass);

        Doesn't it go against RAII? I keep reading that scoped_ptr is RAII but i can use object before it is initialized. Thanks

        A Offline
        A Offline
        Aescleal
        wrote on last edited by
        #3

        So, apart from the fact you'll never be able to call an object class (naughty, naughty) how does writing:

        boost::scoped_ptr ptr;

        leave you with an unitialised object? Or in other words what's not initialised in that statement? If you can work that out you'll see that scoped_ptr supports RAII (or as I prefer to call it these days scoped based resource management, SBRM) perfectly. Cheers, Ash

        E 2 Replies Last reply
        0
        • C Chris Losinger

          and what happens when you run that?

          image processing toolkits | batch image processing

          A Offline
          A Offline
          Aescleal
          wrote on last edited by
          #4

          He won't even get as far as compiling it, let alone running it :-)

          1 Reply Last reply
          0
          • C Chris Losinger

            and what happens when you run that?

            image processing toolkits | batch image processing

            E Offline
            E Offline
            elelont2
            wrote on last edited by
            #5

            I got a segmentation fault, as expected. Could you please explain.

            C 1 Reply Last reply
            0
            • E elelont2

              I got a segmentation fault, as expected. Could you please explain.

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

              you declared a pointer, but never pointed it to an object. what's to explain?

              image processing toolkits | batch image processing

              E 1 Reply Last reply
              0
              • C Chris Losinger

                you declared a pointer, but never pointed it to an object. what's to explain?

                image processing toolkits | batch image processing

                E Offline
                E Offline
                elelont2
                wrote on last edited by
                #7

                Is the purpose of RAII not to allow such a thing? Currently i am acquiring a resource but do not initialize it.

                C 1 Reply Last reply
                0
                • E elelont2

                  Is the purpose of RAII not to allow such a thing? Currently i am acquiring a resource but do not initialize it.

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

                  elelont2 wrote:

                  Currently i am acquiring a resource but do not initialize it.

                  but you're not acquiring any resource; you're just declaring an uninitialized pointer. the scoped_ptr c'tor allows you to defer allocation - but you do need to allocate it at some time, before you start using it. look at boost's examples[^]:

                  boost::scoped_ptr x(new Shoe);

                  they allocate (acquire) the object while declaring the pointer. also:

                  class MyClass {
                  boost::scoped_ptr ptr;
                  public:
                  MyClass() : ptr(new int) { *ptr = 0; }
                  int add_one() { return ++*ptr; }
                  };

                  they declare the pointer, but defer allocation until the 'add_one' call.

                  image processing toolkits | batch image processing

                  1 Reply Last reply
                  0
                  • A Aescleal

                    So, apart from the fact you'll never be able to call an object class (naughty, naughty) how does writing:

                    boost::scoped_ptr ptr;

                    leave you with an unitialised object? Or in other words what's not initialised in that statement? If you can work that out you'll see that scoped_ptr supports RAII (or as I prefer to call it these days scoped based resource management, SBRM) perfectly. Cheers, Ash

                    E Offline
                    E Offline
                    elelont2
                    wrote on last edited by
                    #9

                    I have an uninitialized object in that case and ptr->Func() will probably crash?

                    1 Reply Last reply
                    0
                    • A Aescleal

                      So, apart from the fact you'll never be able to call an object class (naughty, naughty) how does writing:

                      boost::scoped_ptr ptr;

                      leave you with an unitialised object? Or in other words what's not initialised in that statement? If you can work that out you'll see that scoped_ptr supports RAII (or as I prefer to call it these days scoped based resource management, SBRM) perfectly. Cheers, Ash

                      E Offline
                      E Offline
                      elelont2
                      wrote on last edited by
                      #10

                      ny_old_iron class will be uninitialized. How is it initialized? Also, what do you mean by "object class"? RAII says that the object is initialized during acquisition. scoped_ptr is supposed to support RAII but it allows to defer allocation, which means that the object pointed by the scoped_ptr is uninitialized. Therefore i would say that scoped_ptr is not RAII.

                      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