Topic: removing list elements


Author: juergen@monocerus.demon.co.uk (Juergen Heinzl)
Date: 1999/03/18
Raw View
In article <7cj8nk$tmk$1@nnrp1.dejanews.com>, alexxandra@my-dejanews.com wrote:
>
>This problem has been described to me by a fellow developer, and I cannot find
>much insight online or in books.  Using the STL list, we wanted to substitute
>our home-grown implementation of a linked list.  However, we were storing
>(pointers or references to) objects in the linked list and we've run into a
>problem with the code dumping core from the stl section.  Should I assume it
>is the developer's responsibility to remove the object when objects and not
>straight data are being stored in the list?  If so, would a reasonable
>solution be to overload list's remove() to call the destructor on the object
>being removed?
>
>Any insight/input/advice is very welcome.

Just an idea ... do you have a copy constructor and if ... shallow or deep
copy ? For pointers you might consider smart pointers and regarding the STL
containers AFAIK objects must support = and <

Cheers,
Juergen

--
\ Real name     : J   rgen Heinzl                 \       no flames      /
 \ EMail Private : juergen@monocerus.demon.co.uk \ send money instead /
  \ Phone Private : +44 181-332 0750              \                  /



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: alexxandra@my-dejanews.com
Date: 1999/03/15
Raw View
This problem has been described to me by a fellow developer, and I cannot find
much insight online or in books.  Using the STL list, we wanted to substitute
our home-grown implementation of a linked list.  However, we were storing
(pointers or references to) objects in the linked list and we've run into a
problem with the code dumping core from the stl section.  Should I assume it
is the developer's responsibility to remove the object when objects and not
straight data are being stored in the list?  If so, would a reasonable
solution be to overload list's remove() to call the destructor on the object
being removed?

Any insight/input/advice is very welcome.

Thanks,
Alexandra F. Stehman
Computer Scientist
Naval Surface Warfare Center, Dahlgren Division
17320 Dahlgren Road
Dahlgren, VA 22443

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 1999/03/16
Raw View
alexxandra@my-dejanews.com wrote:
>
> This problem has been described to me by a fellow developer, and I cannot find
> much insight online or in books.  Using the STL list, we wanted to substitute
> our home-grown implementation of a linked list.  However, we were storing
> (pointers or references to) objects in the linked list and we've run into a
> problem with the code dumping core from the stl section.  Should I assume it
> is the developer's responsibility to remove the object when objects and not
> straight data are being stored in the list?  If so, would a reasonable
> solution be to overload list's remove() to call the destructor on the object
> being removed?

Standard containers don't pay any attention to what objects they actuall
contain; they don't see the difference between an integer and a pointer,
and they treat them both the same; which is NOT what you want. If you
are allocating memory, and putting pointers to that memory in a standard
container, you have to make sure to deallocate the memory yourself
before allowing the container to delete the memory the pointer is stored
in.

You can't store references in an standard container; the contained
objects must be CopyConstructible, and references don't qualify.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]