Copy on write (COW) smart pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thecount
    New Member
    • Feb 2008
    • 4

    Copy on write (COW) smart pointers

    What implemetation for a copy on write pointer do you recommend that I use, since it's not a completely standard thing? I found the link to http://code.axter.com/cow_ptr.h, used it, and it crashed. I am compiling with visual studio v8.0 without a glitch and I am starting to think I just didn't understand how to use the pointer. Below, I attach a simple program that crashed, but works fine with a copy pointer from http://code.axter.com/copy_ptr.h. Thanks a lot in advance for your help
    Last edited by sicarie; Feb 27 '08, 02:05 PM. Reason: Code removed as it is updated later (and we ask that no full code be posted anyway...)
  • thecount
    New Member
    • Feb 2008
    • 4

    #2
    Sorry and don't accuse me of flooding. I realized there was an error in my sample code -- I had been trying things out and I happen to send the wrong version (although it also crashes, even doing yet less than the one below). It should return

    point[0] = ( 0 , 0 )
    point[1] = ( 1 , 0 )
    point[0] = ( 0 , 0 )
    point[1] = ( 1 , 0 )
    copypoint[0] = ( 49 , 0 )
    copypoint[1] = ( 1 , 0 )

    but the input is
    [code=cpp]
    /* CLASS */
    class point2D
    {
    protected:
    double x_,y_;
    public:
    point2D() : x_(0), y_(0) {}
    point2D(double x, double y) : x_(x), y_(y) {}
    void setx(double x) { x_ = x;}
    void sety(double y) { y_ = y;}
    double getx() const { return x_;}
    double gety() const { return y_;}
    };

    class point3D : public point2D
    {
    private:
    double z_;
    public:
    point3D() : point2D(), z_(0) {}
    point3D(double x, double y, double z) : point2D(x,y), z_(z) {}
    void setz(double z) { z_ = z;}
    double getz() const { return z_;}
    };
    [/code]

    and the main
    [code=cpp]
    std::size_t npoints = 2;
    std::vector<cow _ptr<point2D> > points(npoints) ;

    // initialize

    for(std::size_t i = 0; i < npoints; ++i)
    {
    points[i] = cow_ptr<point2D >(new point3D(i,0,i+1 ));
    }

    // first print to check input

    for(std::size_t i = 0; i < points.size(); ++i)
    {
    std::cout << "point[" << i << "] = ( "
    << points[i]->getx() << " , "
    << points[i]->gety() << " )"
    << std::endl;
    }

    // copy vector and make changes

    std::vector<cow _ptr<point2D> > copypoints = points;
    copypoints[0]->setx(49.0);

    // check that old vector and new vector are not shared

    for(std::size_t i = 0; i < points.size(); ++i)
    {
    std::cout << "point[" << i << "] = ( "
    << points[i]->getx() << " , "
    << points[i]->gety() << " )"
    << std::endl;
    }
    for(std::size_t i = 0; i < copypoints.size (); ++i)
    {
    std::cout << "copypoint[" << i << "] = ( "
    << copypoints[i]->getx() << " , " <<
    copypoints[i]->gety() << " )" <<
    std::endl;
    }
    [/code]
    Sorry about that
    Last edited by sicarie; Feb 27 '08, 02:06 PM. Reason: Code tags are [code=cpp] and after your code [/code]. Please use them. They want to be used. They like to be used.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You might read the article in the C/C++ HowTos about Handle Classes. That article contains a template for a smart pointer that does what you want.

      Comment

      • thecount
        New Member
        • Feb 2008
        • 4

        #4
        Thanks a lot for the answer. I'll try it right away and let you know if it works. Regards

        Comment

        Working...