lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, 29 May 2003, Enrico Colombini wrote:

> On Thursday 29 May 2003 19:07, Thatcher Ulrich wrote:
> > * use a weak pointer when you don't "own" the other object.  E.g. a
> >   child node would have a weak pointer to its parent.  This requires
> >   some awareness on the part of the programmer of when to use a weak
> >   pointer, but at least is safe.
>
> How do you apply this principle to a doubly-linked list, that could
> be kept in existance by a single reference to an unpredictable item
> (e.g. a reference either to the head or to the tail)?

I guess it might look like this:

struct element {
	// some data
	smart_ptr<element>	next;
	weak_ptr<element>	prev;
};

And, the list itself must be terminated by NULL, or be explicitly
broken when you want to collect it.

(In practice, we also use a lot of raw pointers in cases where we care
about the efficiency and we know it's safe.)

> By the way, how does Python fare with doubly-linked lists?

Not sure.  I did discover that the mailing list Basile suggested has a
good thread on reference-counting, starting with this post:

http://lists.tunes.org/archives/gclist/2002-August/002341.html

I also discovered this amusing and typically opinionated posting by
Linus Torvalds on the gcc mailing list:

http://gcc.gnu.org/ml/gcc/2002-08/msg00544.html

I haven't mined the Python or Perl lists for relevant threads, but if
somebody else has, please post links.

-Thatcher