🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #140

Re: Deleting copies of object destroys other copies' contents

#140
From: Terry Milan
Date: Sat, 27 Mar 1999 00:00
74 lines
3584 bytes
From the example that you give, I believe you are our of luck.  Depending
upon which operating system you run under, the results of the second delete
operation tend to undefined.   I believe that you were hoping for counted
memory objects, but native C++ does not provide this.  The STL is adopting
and auto_ptr template, but I think it is only a smart pointer and doesn't
provide memory counting.  I think I had heard that they were considering a
memory counted template similar to auto_ptr, it might be worth checking STL
resources to see if something like this is in the works.

In the meantime, I believe that you are going to have to make a deep copy of
the contained object, thus override the assignment operator.  In the example
that you gave, you use a primitive data type.  If you had a pointer to an
object which you wrote rather than a pointer to an int in your container
class and you wish to have a single copy in memory which is shared by more
than one object, you could write an envelope class that has a static pointer
to your memory mapped object and a static use counter.  Your envelope object
could control the creation and eventual deletion of the memory object with
the use counter by incrementing and decrementing it in the contructors and
destructor.  This is an algorithm that I have used from a book written by
James Coplien, named Advanced C++.  If you can get hold of this book, there
are a few idioms in it that might help you.  My book is quite tattered, so I
have tried this a few times.

However, the real answer to your question is, there is no easy way.

Regards,
Terry




Michael <&&schwager@u.arizona.edu> wrote in message ...
>hey all, I have a question
>
>If I have an object with a bunch of pointers to stuff, and the destructor
of
>that object goes and destroys all the pointers too, then what happens when
I
>have a copy of that object, where only the values of the pointers were
>copied, and then the copy gets destroyed?  How do I save the data the
>pointer pointed to without making it invalid? (and hopefully without having
>to write an operator= for it?
>
>For instance
>
>class Object
>    {
>    int *ptr;
>    ~Object() {delete ptr;};
>    };
>
>Object obj1;
>obj1.ptr = new int;     // say it now == 0x1234567
>
>Object *obj2 = new Object;
>*obj2 = obj1;    // now obj2->ptr == obj1.ptr == 0x1234567 via
>member-to-member (compiler-generated) copy
>
>delete obj2;    // this deletes ptr and makes *ptr bad data
>
>obj1.ptr = ?????? // under MSVC this actually becomes == 0xafafafaf for
easy
>identification  I guess.
>
>
>Is there an easy way to save the value of ptr, or to copy the contents of
>obj1.ptr to obj2->ptr without rewriting the operator= function?
>
>thanks
>ms
>
>
>
>
Technical Details
Message-ID: <7djuf7$9vj$1@juliana.sprynet.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <Fz_K2.8313$_47.7058@news2.giganews.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f79ee,bcdf27bb5f8413b
X-Google-Attributes: gidf79ee,public
X-Google-Thread: 114592,bcdf27bb5f8413b
X-Google-Attributes: gid114592,public
X-Google-Thread: 109fba,bcdf27bb5f8413b
X-Google-Attributes: gid109fba,public
X-Google-Thread: 10d4ba,bcdf27bb5f8413b
X-Google-Attributes: gid10d4ba,public
From: "Terry Milan" <tmilan@sprynet.com>
Subject: Re: Deleting copies of object destroys other copies' contents
Date: 1999/03/27
Message-ID: <7djuf7$9vj$1@juliana.sprynet.com>
X-Deja-AN: 459854551
References: <Fz_K2.8313$_47.7058@news2.giganews.com>
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
Organization: Home PC
Newsgroups: alt.comp.lang.learn.c-c++,att.lang.c++,comp.lang.c++,it.comp.lang.c++