Topic: Does this delete a linked list?


Author: pstemari@erinet.com (Paul J. Ste. Marie)
Date: 1995/07/04
Raw View
In article <3t1q3e$pf@tools.near.net>,
   barmar@nic.near.net (Barry Margolin) wrote:
:In article <DAy50y.1uq@poly.edu> nwo@poly.edu (Nai Wo) writes:
:>How is he able to access the next element in the list when he just deleted
:>the element that was pointing to it?  He claims that the method works just
:>fine -- for one-dimensional linked lists -- but isn't it dangerous?
:
:Yes, it's dangerous, but it will work in many common implementations.  When
:you delete an object, it normally just updates the memory manager's free
:list database, but most implementations don't actually touch the memory
:that the object itself took up (although the object's destructor might).
:The next allocation of an object might reuse this memory, but since he's
:not allocating anything between the deletes he's safe from this.

You got to be careful with this.  A number of environments, when compiling in
a debug mode, will quite deliberately fill deleted memory with trash, just to
catch this sort of thing.  As I recall, VC++ fills it with 0xBF.

 --Paul J. Ste. Marie, pstemari@well.sf.ca.us, pstemari@erinet.com

Contact your Congressman today to oppose the Comunications Decency Act
(S.652, Title IV)!





Author: imp@village.org (Warner Losh)
Date: 1995/07/04
Raw View
In article <3tbq54$ctk@news.erinet.com>,
Paul J. Ste. Marie <pstemari@erinet.com> wrote:
>You got to be careful with this.  A number of environments, when compiling in
>a debug mode, will quite deliberately fill deleted memory with trash, just to
>catch this sort of thing.  As I recall, VC++ fills it with 0xBF.

True.

Also Purify (and products like it) will tell you when you access
memory that has been free()d/delete'd.

As others have pointed out, you also run into problems in a
multi-threaded environment where you could run into something like:
 delete this;
 <thread premted>
  malloc/new called in another thread, getting memory
  just freed
 <thread resumed>
 attempt to read data that isn't what you think it is, program
usually crashes.

I believe the standard (remember the standard, that's what this group
is supposed to be about :-) says that accessing any part of an object
after it has been deleted is undefined.  I don't have chapter and
verse, but I seem to recall seeing it, at least in an earlier draft of
the standard.  The 'C' standard says "The value of a pointer that
refers to freed space is indeterminate" (Sec 4.10.3, pg 155, line 19).

Warner

--
Warner Losh  "VMS Forever"  home: imp@village.org
Cyberspace Development, Inc   work: imp@marketplace.com
Makers of TIA, The Internet Adapter.  http://marketplace.com/





Author: rad6938@gemini.tntech.edu (Rad)
Date: 1995/07/05
Raw View
In article <3t1q3e$pf@tools.near.net>, barmar@nic.near.net (Barry Margolin) writes:
>In article <DAy50y.1uq@poly.edu> nwo@poly.edu (Nai Wo) writes:
>>How is he able to access the next element in the list when he just deleted
>>the element that was pointing to it?  He claims that the method works just
>>fine -- for one-dimensional linked lists -- but isn't it dangerous?
>
>Yes, it's dangerous, but it will work in many common implementations.  When
>you delete an object, it normally just updates the memory manager's free
>list database, but most implementations don't actually touch the memory
>that the object itself took up (although the object's destructor might).
>The next allocation of an object might reuse this memory, but since he's
>not allocating anything between the deletes he's safe from this.

As long as his system isn't multitasking, and no interrupts allocate memory and
...

----------------------------------------------------------------------------
 Richard Deken                   Graduate student in electrical engineering
 PGP public key available      Tennessee Technological University
 Internet: rad6938@gemini.tntech.edu        Cookeville, TN, USA





Author: tblancha@evolving.com (Todd Blanchard)
Date: 1995/07/03
Raw View
Nai Wo (nwo@poly.edu) wrote:
: I was inquiring about the fastest way to delete a linked list, and I saw
: a fellow grad student using a method that seems totally counter-intuitive:
: he used only one pointer, initialized first to the head of the list, he
: then deleted -- by using 'delete' -- the storage associated with that
: pointer expression, and (what seems strange to me) accessed the
: next element in the linked list by following the link given in the
: element that he just deleted.  He does this until the entire linked list
: is deallocated.

: How is he able to access the next element in the list when he just deleted
: the element that was pointing to it?  He claims that the method works just
: fine -- for one-dimensional linked lists -- but isn't it dangerous?

Yes!
This is extremely dangerous and very bad practice.   We frequently test code
with debug versions of new/malloc/delete/free that intentionally trash
free'd memory.  Your friend's code will go down in flames under such a test.
In a process with pre-emptive threads, he's likely to get killed as well.
Especially if the call to free triggers a thread swap and another thread
grabs the memory for its own use before he can read it.

Todd Blanchard





Author: nwo@poly.edu (Nai Wo)
Date: 1995/06/29
Raw View
I was inquiring about the fastest way to delete a linked list, and I saw
a fellow grad student using a method that seems totally counter-intuitive:
he used only one pointer, initialized first to the head of the list, he
then deleted -- by using 'delete' -- the storage associated with that
pointer expression, and (what seems strange to me) accessed the
next element in the linked list by following the link given in the
element that he just deleted.  He does this until the entire linked list
is deallocated.

How is he able to access the next element in the list when he just deleted
the element that was pointing to it?  He claims that the method works just
fine -- for one-dimensional linked lists -- but isn't it dangerous?


Any comments or clarifications will be appreciated.  Please send e-mail
to nwo@photon.poly.edu



----   Nai





Author: barmar@nic.near.net (Barry Margolin)
Date: 1995/06/30
Raw View
In article <DAy50y.1uq@poly.edu> nwo@poly.edu (Nai Wo) writes:
>How is he able to access the next element in the list when he just deleted
>the element that was pointing to it?  He claims that the method works just
>fine -- for one-dimensional linked lists -- but isn't it dangerous?

Yes, it's dangerous, but it will work in many common implementations.  When
you delete an object, it normally just updates the memory manager's free
list database, but most implementations don't actually touch the memory
that the object itself took up (although the object's destructor might).
The next allocation of an object might reuse this memory, but since he's
not allocating anything between the deletes he's safe from this.
--
Barry Margolin
BBN Planet Corporation, Cambridge, MA
barmar@{bbnplanet.com,near.net,nic.near.net}
Phone (617) 873-3126 - Fax (617) 873-5124