#!/usr/local/bin/rune -x -d # # Test heap storage. Rune does not garbage collect loops... that is # considered a run time error (which we will eventually detect). Rune # will track references and delete heap objects recursively. It is # recommended that you not rely on this for long singly-linked lists # because you may run out of stack. Intead you should NULL the next # pointer out manually. # # This example defaults to recursively cleaning up after your list. # If you change the 'i' loop to go to, say, 100000, and the (i == 30) # to be (i == 80000), you may see the stack overflow. import "sys"; import ; alias stdio.File *stdout = stdio.stdout; class Test { Test *next; int x = 4; } Test *Dummy; int main(int ac, char **av) { unlocked Test *base; Test *scan; Test **scrap; Test **pt = &base; int i; for (i = 0; i < 10; ++i) { (*pt).new(); (*pt)->x = i; if (i == 30) { scrap = &base; } if (scrap != scrap.NULL) { Test *tmp = *scrap; *scrap = tmp->next; # tmp is NULL'd on semantic exit from block, since # nobody is pointing to it that should trip a # recursive dereference of tmp->next. # #tmp->next = NULL; scrap = &(*scrap)->next; } pt = &(*pt)->next; } for (scan = base; scan != Dummy.NULL; scan = scan->next) { stdout->show(scan->x); } while ((scan = base) != Dummy.NULL) { base = scan->next; scan->next = NULL; scan = NULL; } return(0); }