唐彬 <tb731685340@gmail.com>: Dec 27 10:42PM -0800 #include <iostream> using namespace std; class Smart_Ptr; class U_Ptr { friend class Smart_Ptr; private: U_Ptr(int *_p): p(_p), use(1) { cout << "U_Ptr constructor is called !" << endl; } ~U_Ptr() { cout << "U_Ptr destructor is called!" << endl; delete p; } int use; int *p; }; class Smart_Ptr { public: Smart_Ptr(int *_p): ptr(new U_Ptr(_p)) { cout << "Smart_Ptr constructor is called !" << endl; } ~Smart_Ptr() { cout << "Smart_Ptr destructor is called !" << endl; if(--ptr->use == 0) { delete ptr; } } int get_int() const { return *ptr->p; } private: U_Ptr *ptr; }; void test() { int a = 8; Smart_Ptr obj(&a); cout << "int val of obj a is " << obj.get_int() << endl; return ; } int main() { cout << "now testing my Smart_Ptr:" << endl; test(); return 0; } |
Paavo Helde <myfirstname@osa.pri.ee>: Dec 28 12:44PM +0200 On 28.12.2017 8:42, 唐彬 wrote: > private: > U_Ptr *ptr; > }; You have not defined any copy constructor for your classes, so compiler generates them automatically, but gets it wrong. Ditto for the assignment op, move ctor and move assignment op. You should either define them or declare them as deleted if they are not needed. Another tell-tale of the same thing is that your code contains code for decrementing the refcounter, but not for incrementing. See http://en.cppreference.com/w/cpp/language/rule_of_three for details. I also hope you are aware C++ already contains std::shared_ptr and this is just a learning exercise. Cheers Paavo |
Marcel Mueller <news.5.maazl@spamgourmet.org>: Dec 28 11:50AM +0100 On 28.12.17 07.42, 唐彬 wrote: > Smart_Ptr(int *_p): ptr(new U_Ptr(_p)) { > cout << "Smart_Ptr constructor is called !" << endl; > } First of all the copy constructor is missing. So ->use is not incremented in this case. This will decrement ->use multiple times on destruction. On the first invocation the underlying object is destroyed. Any further access is after the memory has been released, i.e. UB. > void test() { > int a = 8; > Smart_Ptr obj(&a); Secondly you taking the address of an object on the stack here and pass it to a smart pointer that takes the ownership. The resulting call to delete will sadly fail because the object is owned by the stack rather than the heap. Again UB. > cout << "int val of obj a is " << obj.get_int() << endl; > return ; > } Here it crashes because the destructor invokes delete &a; BTW. it is amazingly complex to write real safe reference counted smart pointers, especially if multiple threads come into play. And it's more complex to write a smart pointer which is still pretty fast. Have a look at the implementation of std::shared_ptr to get an impression of it. And it is even more complex to write a reference counted smart pointer that provides strong thread safety, i.e. get access to a valid object from a global smart pointer instance that changes asynchronously. Not even std::shared_ptr supports this pattern. Marcel |
Ben Bacarisse <ben.usenet@bsb.me.uk>: Dec 28 10:52AM > #include <iostream> > using namespace std; ... more code What did you try to find out what's wrong? Did you, for example, try to simplify the example? Does this version make the error clearer to you: #include <iostream> class U_Ptr { public: U_Ptr(int *_p): p(_p) { } ~U_Ptr() { std::cout << "U_Ptr destructor is called!" << std::endl; delete p; } int *p; }; int main() { int a = 8; U_Ptr obj(&a); return 0; } If not, try to remove the class altogether whilst leaving the actions that the class performs intact. That should make it clear! -- Ben. |
schwarz45@yahoo.com: Dec 28 09:42AM -0800 On Thursday, December 28, 2017 at 2:52:10 AM UTC-8, Ben Bacarisse wrote: > that the class performs intact. That should make it clear! > -- > Ben. In your example, the failure occurs on the statement delete p; because p does not point to memory allocated by new. In the OP's code, the failure occurs in ~Smart_Ptr on the statement delete ptr; but in this case ptr DOES point to memory allocated by new (courtesy of the initialization performed by the U_Ptr constructor). At no point is ptr modified. It seems like ~Smart_Ptr should have no trouble calling ~U_Ptr for the object ptr points to. Yes, ~U_Ptr will fail when it tries to delete p but the current failure occurs before that destructor begins to execute. |
Ben Bacarisse <ben.usenet@bsb.me.uk>: Dec 28 08:04PM > trouble calling ~U_Ptr for the object ptr points to. > Yes, ~U_Ptr will fail when it tries to delete p but the current > failure occurs before that destructor begins to execute. Not when I run it -- the failure is in U_Ptr::~U_Ptr called from Smart_Ptr::~Smart_Ptr. Are you saying the OP's code (which as has been pointed out does have other problems) is failing for some other reason? -- Ben. |
Barry Schwarz <schwarzb@dqel.com>: Dec 28 02:12PM -0800 On Thu, 28 Dec 2017 20:04:48 +0000, Ben Bacarisse >schwarz45@yahoo.com writes: <snip> >Not when I run it -- the failure is in U_Ptr::~U_Ptr called from >Smart_Ptr::~Smart_Ptr. Are you saying the OP's code (which as has been >pointed out does have other problems) is failing for some other reason? My mistake. The debugger I am using will step into a destructor called as the result of a return statement but not when called by a delete statement. By placing breakpoints in all the destructors I was able to verify that ~Smart_Ptr successfully calls ~U_Ptr and that fails on the delete p statement as you previously noted. -- Remove del for email |
mcheung63@gmail.com: Dec 28 10:36AM -0800 Rick, why you stop? post some holy shit in thie group pls |
Bonita Montero <Bonita.Montero@gmail.com>: Dec 28 11:56AM +0100 There you can download a good book on the C++ standard-library: http://bit.ly/2C2o0yr |
woodbrian77@gmail.com: Dec 28 08:29AM -0800 On Wednesday, December 27, 2017 at 2:28:02 PM UTC-6, Paavo Helde wrote: > Congrats Brian, you have found a (hopefully rare) C++ tutorial which > seems to contain *no* mention of std::vector, std::list, etc, despite > what OP asked for! I think it's becoming more obvious that on-line code generation is the way things have to be. That's my explanation for the little vultures here that want to see me fall. Brian Ebenezer Enterprises - Enjoying programming again. http://webEbenezer.net |
Mr Flibble <flibble@i42.co.uk>: Dec 28 05:50PM > generation is the way things have to be. > That's my explanation for the little vultures here that > want to see me fall. Another explanation is that you are simply deluded. This explanation I find more likely. /Flibble |
woodbrian77@gmail.com: Dec 28 10:14AM -0800 On Thursday, December 28, 2017 at 11:51:16 AM UTC-6, Mr Flibble wrote: > > want to see me fall. > Another explanation is that you are simply deluded. This explanation I > find more likely. Another vulture heard from. |
legalize+jeeves@mail.xmission.com (Richard): Dec 28 06:28PM [Please do not mail me a copy of your followup] =?UTF-8?B?5ZSQ5b2s?= <tb731685340@gmail.com> spake the secret code >where can i find the code or tutorial to learn from, which can better my >coding style and let me learn more about how they work? It is unclear if you are asking how to *implement* std::vector, etc., or if you are asking how to *use* the standard library. For the latter, I recommend Nicolai Josuttis' book "The C++ Standard Library: A Tutorial and Reference" (2nd ed.) <http://amzn.to/2DsmXnv> For the former, I would recommend any basic book on algorithms and data structures. There are plenty of these as they are core computer science and not specific to C++. -- "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline> The Terminals Wiki <http://terminals-wiki.org> The Computer Graphics Museum <http://computergraphicsmuseum.org> Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com> |
legalize+jeeves@mail.xmission.com (Richard): Dec 28 06:25PM [Please do not mail me a copy of your followup] Andrey Karpov <karpov2007@gmail.com> spake the secret code >project are regarded, as well as treatment of a disease "fixed one >header - a half of a project was rebuilt." >Continue read: https://www.viva64.com/en/b/0549/ With regards to breaking dependencies, I found this lightning talk interesting: CppCon 2017: Charles L. Wilcox "The Three Layers of Headers A Curious Discovery" <https://www.youtube.com/watch?v=su9ittf-ozk> -- "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline> The Terminals Wiki <http://terminals-wiki.org> The Computer Graphics Museum <http://computergraphicsmuseum.org> Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com> |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment