Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #3164

Re: Destructors can copying objects

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news2.arglkargh.de!news.n-ix.net!news.belwue.de!news-zh.switch.ch!kanaga.switch.ch!switch.ch!newsfeed-00.mathworks.com!panix!not-for-mail
From Ruben Safir <ruben@mrbrklyn.com>
Newsgroups comp.lang.c++
Subject Re: Destructors can copying objects
Date Tue, 5 Apr 2011 15:40:34 +0000 (UTC)
Organization PANIX Public Access Internet and UNIX, NYC
Lines 77
Message-ID <infd5i$hvf$1@reader1.panix.com> (permalink)
References <in76i3$105$2@reader1.panix.com> <incf69$9fr$1@dont-email.me> <indbq3$r87$2@reader1.panix.com> <ineusg$pf4$1@dont-email.me>
NNTP-Posting-Host www2.mrbrklyn.com
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
X-Trace reader1.panix.com 1302018034 18415 96.57.23.82 (5 Apr 2011 15:40:34 GMT)
X-Complaints-To abuse@panix.com
NNTP-Posting-Date Tue, 5 Apr 2011 15:40:34 +0000 (UTC)
X-Blackjet Blackjet is a Yankee Fan
X-DRMisTHEFT Use GNU Linux today
X-From A Dark Cloud
X-LOCATION Brooklyn NY - Forget abou' it!
X-NYLXS Really - yah think computers are supposed to be broken?
User-Agent Pan/0.133 (House of Butterflies)
Xref x330-a1.tempe.blueboxinc.net comp.lang.c++:3164

Show key headers only | View raw


On Tue, 05 Apr 2011 11:36:48 +0000, Yannick Tremblay wrote:

> On 2011-04-04, Ruben Safir <ruben@mrbrklyn.com> wrote:
>> On Mon, 04 Apr 2011 12:56:41 +0000, Yannick Tremblay wrote:
>>
>>>> ======================================= /* constructor */ Node<unk>(
>>>> unk value, Node<unk> *item_to_link_to = 0); Node ( const Node &other
>>>> ); /* copy constructor - Need to write this! */
>>> 
>>> Stop here!
>>> 
>>> Could you clarify to us why the following comments is there: /* copy
>>> constructor - Need to write this! */
>>
>> Because I haven't written it yet.
>>
>> The purpose of the question is to understand what is happening, not to
>> necessarily fix the program.  This is not a homework assignment with a
>> deadline.  I just want to have a better understanding of why the
>> destructor caused a double delete.
> 
> This was a leading question.
> 
> copy-constructor are not always needed for all classes.  However, for
> some classes a copy constructor is absolutely needed.
> 
> Here you have a header file that explicitely says (correctly given the
> your class contains raw pointers) that the copy contructor need to be
> written.  I am not sure if you wrote it yourself as a reminder or if it
> was a format that was specified externally from a template that attempt
> to remind peoples that they should write copy-constructors.

Thanks for replying.

It so happens, that in this case, the original linked list example in 
Lippman privatized the copy constructor and assignment operator in order 
to prevent them from being called.  When I branched off the original 
example, I decided that I'd write a copy constructor so that I can define 
that behavior, but I haven't gotten to it.  I did not know at that time 
that this would be something that would cause such a bug in the program 
that it was a vital need to be addressed.

But I'm distressed at this point because I don't want two copies of all 
the nodes.  I want one series of nodes, which are created in free memory 
on the stack, handled in all possible scopes.  The nodes themselves are 
'self aware" of their positions in the list in that they have the 
information of they're position in the nodes.  The List object itself 
does nothing more that communicate with the nodes.  If I copy the List 
Object, I don't want to duplicate the nodes as well.  They're on the 
heap, and then can stay there.

> 
> Regardless, despite the presence of this reminder, you have not written
> the copy-constructor. That should very probably be your first point of
> call.
> 
> Your destructor cause double delete because your copy constructor and
> your destructor are incompatible.  Write a copy-constructor that is
> compatible with your destructor or write a copy-constructor and modify
> your destructor so both are compatible and you will not have a bug
> anymore.
> 
> So essentially you have a Node that contains a pointer to value and a
> pointer to next.  You have a destructor that delete the pointer to
> value.
> 
> What currently happens if you copy a node? (hint: compiler generated
> copy constructor) (hint: what will be the values of value_ and next_ in
> the copy and in the original?)
> 
> What will happen if you delete this node copy?
> 
> What will happen if you delete the original node?
> 
> 
> Yannick

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-02 12:58 +0000
  Re: Destructors can copying objects Volker Lukas <vlukas@gmx.de> - 2011-04-02 15:29 +0200
    Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-03 02:59 +0000
      Re: Destructors can copying objects "Balog Pal" <pasa@lib.hu> - 2011-04-03 11:32 +0200
        Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-03 12:29 +0100
          Re: Destructors can copying objects Bart van Ingen Schenau <bart@ingen.ddns.info> - 2011-04-04 13:43 +0200
            Re: Destructors can copying objects Michael Doubez <michael.doubez@free.fr> - 2011-04-05 01:26 -0700
          Re: Destructors can copying objects Joshua Maurice <joshuamaurice@gmail.com> - 2011-04-04 14:03 -0700
            Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:10 +0000
            Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-04 22:28 +0100
          Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:07 +0000
            Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-04 22:31 +0100
              Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-04 22:43 +0100
              Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 22:38 +0000
                Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-04 23:42 +0100
                Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 22:54 +0000
      Re: Destructors can copying objects James Kanze <james.kanze@gmail.com> - 2011-04-03 10:07 -0700
        Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:14 +0000
        Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:17 +0000
          Re: Destructors can copying objects LR <lruss@superlink.net> - 2011-04-05 01:52 -0400
    Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-03 03:08 +0000
      Re: Destructors can copying objects Paavo Helde <myfirstname@osa.pri.ee> - 2011-04-03 02:11 -0500
      Re: Destructors can copying objects Volker Lukas <vlukas@gmx.de> - 2011-04-03 12:02 +0200
      Re: Destructors can copying objects Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2011-04-03 12:14 +0200
        Re: Destructors can copying objects Roberto Waltman <usenet@rwaltman.com> - 2011-04-03 18:16 -0400
          Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-15 23:06 +0000
            Re: Destructors can copying objects LR <lruss@superlink.net> - 2011-04-15 19:19 -0400
        Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-15 23:03 +0000
          Re: Destructors can copying objects "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-16 01:47 +0200
            Re: Destructors can copying objects Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2011-04-16 13:00 +0200
              Re: Destructors can copying objects "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-16 15:57 +0200
  Re: Destructors can copying objects LR <lruss@superlink.net> - 2011-04-02 09:51 -0400
  Re: Destructors can copying objects Yannick Tremblay <yannick@tremnblay.co.uk> - 2011-04-04 12:56 +0000
    Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:05 +0000
      Re: Destructors can copying objects Paavo Helde <myfirstname@osa.pri.ee> - 2011-04-04 21:56 -0500
      Re: Destructors can copying objects Yannick Tremblay <yannick@tremnblay.co.uk> - 2011-04-05 11:36 +0000
        Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-05 15:40 +0000
          Re: Destructors can copying objects Sana <sanatakos@gmail.com> - 2011-04-05 10:11 -0700
          Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-05 20:28 +0000
          Re: Destructors can copying objects Yannick Tremblay <yannick@tremnblay.co.uk> - 2011-04-06 11:12 +0000
      Re: Destructors can copying objects drew@furrfu.invalid (Drew Lawson) - 2011-04-05 13:02 +0000
        Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-05 15:51 +0000
          Re: Destructors can copying objects drew@furrfu.invalid (Drew Lawson) - 2011-04-05 17:47 +0000
            Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-05 20:25 +0000
              Re: Destructors can copying objects drew@furrfu.invalid (Drew Lawson) - 2011-04-05 20:37 +0000

csiph-web