473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 4527
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,208 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

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

Similar topics

16
by: bluekite2000 | last post by:
I want Matrix A(B) to create shallow copy of B but A=B to create deep copy of B. Is that bad design? Why and why not?
7
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For...
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
11
by: PengYu.UT | last post by:
The following program calls the normal constructor and the copy constructor. By calling the copy constuctor is redundandant, all I want is only a vector of a trial object. Is there any way to...
9
by: utab | last post by:
Dear all, How do experienced programmers using this group use the rule of thumb,namely copy ctor, assignment operator, and dtor. example if the class does not need them dont use and define...
3
by: jan247 | last post by:
Hi guys, thanks for taking time to read this.. Question 1 Let A be the superclass and B the subclass A is not an abstract class. print is a virtual function of A, which B implements. A a =...
3
by: mosfet | last post by:
Hi, I read an article about COW implementation and issues in multithreaded environments. Then I found another article about counted reference and now I don't understand anything. For instance...
19
by: Angus | last post by:
I have a socket class CTestClientSocket which I am using to simulate load testing. I create multiple instances of the client like this: for (int i = 0; i < 5; i++) { CTestClientSocket* pTemp...
10
by: Alex Snast | last post by:
hello I'm trying to implement a copy constructor for a vector of pointers to a base class (which is abstract) My question is how would i know what kind of new type should i allocate since the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.