gordon%stats.ucl.ac.uk@NSS.CS.UCL.AC.UK (Gordon Joly and Paul Otto) (03/21/89)
G++ and libg++ 1.34.0 with config.g++ sun3 on a SUN-3/160 with gcc 1.34. Bug is twofold: (a) I think that this code should compile OK (b) in any case, the compiler should not dump core The following script was done on a SUN-3/75 running SunOS 3.4: Script started on Tue Mar 21 14:54:15 1989 io% g++ -v g++ version 1.34.0 io% g++ -c fib.cc In function struct Integer fib (int): fib.cc:17: Segmentation violation Program c++ got fatal signal 11. io% ls -l total 1486 - -rw------- 1 otto 313 Mar 21 08:53 Makefile - -rw------- 1 otto 3745 Mar 21 14:55 bug1 - -rw------- 1 otto 0 Mar 21 14:59 bug2 - -rw-r--r-- 1 otto 1491770 Mar 21 15:01 core - -rw------- 1 otto 507 Mar 21 14:51 fib.bug1.cc - -rw------- 1 otto 507 Mar 21 09:05 fib.cc - -rw------- 1 otto 375 Mar 21 09:04 runfib.cc - -rw------- 1 otto 3223 Mar 21 09:01 table.cc - -rw------- 1 otto 1638 Mar 21 09:02 table.h io% cat fib.cc // // Example for B11b - of use of tables. // // A memo-ising function - using doubly recursive fibonacci as example // // G. P. Otto, 1987 #include "table.h" static table fibresults; // table of known results Integer fib(int n) { Integer res = fibresults.lookup(n); // lookup answer if (!fibresults.lastlookupOK){ // if not yet known res = (n < 2)? 1 : (fib(n-1) + fib(n-2)); // calculate it fibresults.insert(n,res); // and note the result } return(res); } // PS didn't use "dispose"! io% cat table.h // // a simple "table" class, written for B11b by G. P. Otto, 1987 // // In this version, the public interface lacks any way of traversing // the table, so if you don't know what you're looking for, you can't // find it. // // The data-type for "keytype" is almost arbitrary, but you must be able // to check equality. (Actually assume that "==" works for keys.) // IMPORTANT: the hash function needs to be re-designed for some different // types & frequencies of keys. (See table.cc) // // NEED: declarations for "keytype" & "datumtype" data-types typedef int keytype; #include <Integer.h> typedef Integer datumtype; struct cell { // structure to hold data on a linked list keytype key; datumtype datum; cell *next; // pointer to next cell }; static const int hash_tab_size = 97; // hash table size - usually best to make it a prime class table { cell * list[hash_tab_size]; // each list holds the data whose // keys hash to that array index public: table(); // create an empty table ~table(); // destroy a table, reclaiming // space & tidying up as necessary void insert(keytype, datumtype); // "table[key] = ..." // overwrite previous entry if // there is one with same key. // error message if full. datumtype lookup(keytype); // "... = table[key]" // errors (such as no item with // specified key, are handled by // "lastlookupOK", below. int lastlookupOK; // true if last "lookup" succeeded, // false otherwise void dispose(keytype); // delete (key, data) pair from table. // No-op if there isn't such a pair. // Wot! No traversal? }; io% ^D script done on Tue Mar 21 14:56:25 1989 ------- End of Forwarded Message