33 lines
1.0 KiB
QBasic
33 lines
1.0 KiB
QBasic
|
|
10 REM A strict pointer is a distinct declared kind. Assignment always copies;
|
||
|
|
20 REM POINT is the only way to share, so a program that never writes it can
|
||
|
|
30 REM never be surprised by aliasing.
|
||
|
|
40 TYPE NODE
|
||
|
|
50 COUNT#
|
||
|
|
60 TAIL@ AS PTR TO NODE
|
||
|
|
70 END TYPE
|
||
|
|
80 DIM N1@ AS NODE
|
||
|
|
90 DIM N2@ AS NODE
|
||
|
|
100 DIM N3@ AS NODE
|
||
|
|
110 N1@.COUNT# = 10
|
||
|
|
120 N2@.COUNT# = 20
|
||
|
|
130 N3@.COUNT# = 30
|
||
|
|
140 REM A TYPE may refer to itself only through PTR TO, which is what makes a
|
||
|
|
150 REM list possible at all -- by value it would have no finite size.
|
||
|
|
160 POINT N1@.TAIL@ AT N2@
|
||
|
|
170 POINT N2@.TAIL@ AT N3@
|
||
|
|
180 DIM WALK@ AS PTR TO NODE
|
||
|
|
190 POINT WALK@ AT N1@
|
||
|
|
200 PRINT WALK@->COUNT#
|
||
|
|
210 WALK@ = WALK@->TAIL@
|
||
|
|
220 PRINT WALK@->COUNT#
|
||
|
|
230 WALK@ = WALK@->TAIL@
|
||
|
|
240 PRINT WALK@->COUNT#
|
||
|
|
250 REM An unbound pointer is NOTHING, not a crash.
|
||
|
|
260 PRINT WALK@->TAIL@
|
||
|
|
270 REM Writing through a pointer changes what it points at.
|
||
|
|
280 POINT WALK@ AT N1@
|
||
|
|
290 WALK@->COUNT# = 99
|
||
|
|
300 PRINT N1@.COUNT#
|
||
|
|
310 REM And the whole list renders, following pointers as it goes.
|
||
|
|
320 PRINT N1@
|