User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 455,964 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,630 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 789 | Replies: 8
Reply
Join Date: Nov 2007
Location: Nuernberg, GER
Posts: 7
Reputation: teddyzhai is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
teddyzhai teddyzhai is offline Offline
Newbie Poster

Member function copy() ???

  #1  
Nov 21st, 2007
Hi , There:
I just have a simple question by programming.
Looks like this:
  1. class A{
  2. .....
  3. .....
  4. Public:
  5. copy(){
  6. return *(this);
  7. }
  8.  
  9. }

in .cc file: for example we have a container
  1. list<A> list;
  2. A* a = new A(...);
  3. list.push_back(a->copy());

Why is this copy() better than not use it .
I guess, if we use this copy(), then needn't to free this pointer.
But really don't know ,why ......



Any suggestion will be appreciated .

Thx
Teddy
Make Parallel Life feasible.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Member function copy() ???

  #2  
Nov 21st, 2007
The default copy constructor does only minimal thigs -- it is not sufficnet for most classes that contain objects of complex types such as pointers and container classes.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Member function copy() ???

  #3  
Nov 21st, 2007
I don't know where you got that code, but it is wholly ineffective. It uses the copy constructor whether you like it or not.

Further, you left the return type off of the copy() function (so it wouldn't compile). If you declare it as:
A &copy();
then you'll wind-up with two copies of a. If you declare it as:
A copy();
you'll produce three copies of a (one a temporary used in the assignment).

In all cases, the first a that you created with new still needs to be explicitly deleted. Otherwise you'll have a memory leak.

The problem with the default copy constructor that Ancient Dragon alluded to is that it makes a byte-for-byte copy of your object. If your object news variables, etc., then what you get using the default copy constructor is two objects that use the exact same data. So what happens if one object deletes that data? Then the other tries to use or delete it, and your program crashes.

If you want to read more on this, google "shallow vs deep copy".

BTW. Don't name lists "list" or arrays "array".

Oh, yeah, also. If you really want to just pass a pointer off, use a list of pointers:
list<A*> ls;
A *a = new A( ... );
ls.push_back( a );
This works just fine. Your object is never copied (the pointer to the object is copied). You still have to remember to delete each object in the list, though.

Hope this helps.
Reply With Quote  
Join Date: Nov 2007
Location: Nuernberg, GER
Posts: 7
Reputation: teddyzhai is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
teddyzhai teddyzhai is offline Offline
Newbie Poster

Re: Member function copy() ???

  #4  
Nov 21st, 2007
Thanks a lot for both ur help !!
I think i have gotten what u meant.
"Deeply Copy" makes me first understood.
And,
Here, If i have this:

A* a = new A(...);
ls.push_back(a->copy());
delete a;

That means i just push a Object in container, instead of Pointer.
Right ??? then, it works with no mem leaks.

Thx
Teddy
Last edited by teddyzhai : Nov 21st, 2007 at 7:35 pm.
Make Parallel Life feasible.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Member function copy() ???

  #5  
Nov 21st, 2007
Sure. That works.
Last edited by Duoas : Nov 21st, 2007 at 10:21 pm.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Member function copy() ???

  #6  
Nov 21st, 2007
Originally Posted by Duoas View Post
Sure. That works.


Does it? push_back only makes a copy of the pointer, the memory is still allocated to the original pointer which is deleted immediately after the push_back. Now list has an invalid pointer and any attempt to dereference it will fail.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Member function copy() ???

  #7  
Nov 22nd, 2007
No, the whole point is that copy() returns an implicitly copy-constructed copy of *a. Hence, *a is completely out of the equation by the time push_back() gets its piece.

Again, this is assuming copy() returns either A or A& (consistent with the return *(this); line above).

And again, as AD said, if A has any deep data at all, this will still fail, because the data that the copies of *a point to is the same data that *a points to. Once you delete a the deep data disappears also, leaving all copies of *a pointing to invalid data.

Here's an example to play with.
  1. #include <iostream>
  2. #include <string>
  3. #include <list>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7. int counter = 0;
  8.  
  9. class A {
  10. private:
  11. char *deep_data;
  12. int count;
  13.  
  14. public:
  15. A( bool make_me_deep ) {
  16. count = counter++;
  17. cout << "Constructor: " << count << " as " << (make_me_deep ? "deep." : "shallow.") << endl;
  18.  
  19. if (make_me_deep) {
  20. deep_data = new char[ 1000 ];
  21. strcpy( deep_data, "Knight to C3" );
  22. }
  23. else deep_data = NULL;
  24. }
  25.  
  26. ~A() {
  27. cout << "Destructor: " << count << endl;
  28. if (deep_data) delete[] deep_data;
  29. }
  30.  
  31. A( const A& a ) {
  32. // simulate default copy constructor
  33. deep_data = a.deep_data;
  34. count = a.count + 10;
  35.  
  36. cout << "copied " << a.count << " as " << count << endl;
  37. }
  38.  
  39. // A copy() {
  40. A &copy() {
  41. return *(this);
  42. }
  43.  
  44. void hello() {
  45. char *s;
  46. if (deep_data) s = deep_data;
  47. else s = "Does that cloud look like a flower to you?";
  48. cout << endl << count << " says \"" << s << '\"' << endl;
  49. }
  50.  
  51. void deepmodify() {
  52. sprintf( deep_data, "%s%d%s",
  53. "PWND BY ",
  54. count,
  55. "!!!!"
  56. );
  57. }
  58. };
  59.  
  60. int main() {
  61. list<A> ls;
  62. A *a;
  63.  
  64. cout << "(Create shallow and shallow copy it)" << endl;
  65. a = new A( false );
  66.  
  67. ls.push_back( a->copy() );
  68.  
  69. delete a;
  70.  
  71. ls.front().hello();
  72.  
  73.  
  74. cout << "\n(Create deep and shallow copy it)" << endl;
  75. a = new A( true );
  76.  
  77. ls.push_back( a->copy() );
  78.  
  79. ls.back().hello();
  80.  
  81. cout << "\n(Modify the original deep here)" << endl;
  82. a->deepmodify();
  83.  
  84. ls.back().hello();
  85.  
  86. cout << "\n(Delete the original deep)" << endl;
  87. delete a; // Danger Will Robinson!
  88.  
  89. ls.back().hello();
  90.  
  91. cout << "\n(Well, that last statement from 11 should have" << endl
  92. << "caused a core-dump. Lucky you you're still here." << endl
  93. << "So, enjoy the automatic cleanup of the list<A>):" << endl;
  94.  
  95. return 0;
  96. }
Somehow it survives to the end on my XP box, but that's undefined behavior. (Something keeps it from faulting when attempting to read the deleteed data on my sys...)

In any case, list<> understands that it keeps a pointer to things, and manages to handle stuff sufficiently transparently to the user. So if you pass it a reference you don't need to worry about freeing it...

Hope this helps.
Reply With Quote  
Join Date: Nov 2007
Posts: 2
Reputation: thirumalt is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
thirumalt thirumalt is offline Offline
Newbie Poster

Solution Re: Member function copy() ???

  #8  
Nov 22nd, 2007
Hi,
you writhing copy function.. in that function we should mantain the parameter as const bcoz before executing any context switch will occer that any thread can modify the members of that object...

and this copy function is provided by compiler... if your writing means your overwritting that function... internally it ll return the this pointer we no need to return the this pointer externally...
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Member function copy() ???

  #9  
Nov 22nd, 2007
you writhing copy function.. in that function we should mantain the parameter as const bcoz before executing any context switch will occer that any thread can modify the members of that object...
? What are you talking about? (Threads? You should know better than to share objects across threads!)

and this copy function is provided by compiler... if your writing means your overwritting that function... internally it ll return the this pointer we no need to return the this pointer externally...
Now you've totally lost me. Are you talking about A( const A& a ) or A &copy() ?

"Internally" it does exactly what it says.
this is a pointer to myself.
*this is me.
If I return this I get another pointer to myself.
If I return *this I get another me.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:02 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC