home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gumby!wupost!think.com!barmar
- From: barmar@think.com (Barry Margolin)
- Newsgroups: comp.lang.c++
- Subject: Doubly-linked list trick (was Re: Garbage Collection for C++)
- Date: 17 Aug 1992 19:49:54 GMT
- Organization: Thinking Machines Corporation, Cambridge MA, USA
- Lines: 31
- Message-ID: <16ovt2INN3nq@early-bird.think.com>
- References: <1992Aug14.021547.15215@news.mentorg.com>> <DAVEG.92Aug14194411@synaptx.synaptics.com> <2009@appli.se>
- NNTP-Posting-Host: telecaster.think.com
-
- In article <2009@appli.se> niklas@appli.se (Niklas Hallqvist) writes:
- >When you want to do a doubly linked list to the cost of a singly
- >linked one (i.e. in mem. space). Instead of having two pointers
- >for the link in each node, take the "previous" and "next" pointers
- >and XOR them, and store the result!
-
- Problems:
-
- 1) XOR isn't defined for pointers in C/C++.
-
- 2) You could cast the pointers to suitably large integers and XOR those,
- but the result of casting the integers back into pointers is
- implementation-dependent or undefined (I don't remember which, but either
- way it's not portable).
-
- 3) It's only useful if you always traverse the list from one end or the
- other, since you need to know what to XOR each combined pointer with in
- order to get one of the original pointers. But one of the main reasons for
- using a doubly-linked list is so that you can start with any entry in the
- list and get to the rest.
-
- 3a) In particular, how would you do a circular doubly-linked list with this
- scheme? When it starts out, the node's previous and next pointers would
- both be the same (they point to itself) and the result of the XOR would be
- 0. But that's also the result when the circular list has two elements,
- since the previous and next nodes would always be the "other" node.
- --
- Barry Margolin
- System Manager, Thinking Machines Corp.
-
- barmar@think.com {uunet,harvard}!think!barmar
-