468,484 Members | 2,155 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

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
Feb 22 '08 #1
3 4218
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
Expand|Select|Wrap|Line Numbers
  1. /*   CLASS    */
  2. class point2D
  3. {
  4. protected:
  5.     double x_,y_;
  6. public:
  7.     point2D() : x_(0), y_(0) {}
  8.     point2D(double x, double y) : x_(x), y_(y) {}
  9.     void setx(double x) { x_ = x;}
  10.     void sety(double y) { y_ = y;}
  11.     double getx() const { return x_;}
  12.     double gety() const { return y_;}
  13. };
  14.  
  15. class point3D : public point2D
  16. {
  17. private:
  18.     double z_;
  19. public:
  20.     point3D() : point2D(), z_(0) {}
  21.     point3D(double x, double y, double z) : point2D(x,y), z_(z) {}
  22.     void setz(double z) { z_ = z;}
  23.     double getz() const { return z_;}
  24. };
  25.  
and the main
Expand|Select|Wrap|Line Numbers
  1.     std::size_t npoints = 2;
  2.     std::vector<cow_ptr<point2D> > points(npoints);
  3.  
  4.     // initialize 
  5.  
  6.     for(std::size_t i = 0; i < npoints; ++i)
  7.     {
  8.         points[i] = cow_ptr<point2D>(new point3D(i,0,i+1));
  9.     }
  10.  
  11.     // first print to check input
  12.  
  13.     for(std::size_t i = 0; i < points.size(); ++i)
  14.     {
  15.         std::cout << "point[" << i << "] = ( " 
  16.             << points[i]->getx() << " , " 
  17.             << points[i]->gety() << " )" 
  18.             << std::endl;
  19.     }
  20.  
  21.     // copy vector and make changes
  22.  
  23.     std::vector<cow_ptr<point2D> > copypoints = points;
  24.     copypoints[0]->setx(49.0);
  25.  
  26.     // check that old vector and new vector are not shared
  27.  
  28.     for(std::size_t i = 0; i < points.size(); ++i)
  29.     {
  30.         std::cout << "point[" << i << "] = ( " 
  31.             << points[i]->getx() << " , " 
  32.             << points[i]->gety() << " )" 
  33.             << std::endl;
  34.     }
  35.     for(std::size_t i = 0; i < copypoints.size(); ++i)
  36.     {
  37.         std::cout << "copypoint[" << i << "] = ( " 
  38.             << copypoints[i]->getx() << " , " << 
  39.             copypoints[i]->gety() << " )" << 
  40.             std::endl;
  41.     }
  42.  
Sorry about that
Feb 22 '08 #2
weaknessforcats
9,207 Expert Mod 8TB
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.
Feb 22 '08 #3
Thanks a lot for the answer. I'll try it right away and let you know if it works. Regards
Feb 27 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by simkn | last post: by
17 posts views Thread by baibaichen | last post: by
9 posts views Thread by utab | last post: by
3 posts views Thread by jan247 | last post: by
19 posts views Thread by Angus | last post: by
10 posts views Thread by Alex Snast | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.