190 likes | 302 Views
Tips For Programming & Simulation. Wenguang Wang March 24, 2000. General Principles. Catching the bugs automatically. Checking Bugs -- Compiler. Turn on all warnings gcc -Wall myfile.cpp. while (n<total); { ... }. if (a=1). while ((*p1++=*p2++)!=NULL) NULL;.
E N D
Tips For Programming & Simulation Wenguang Wang March 24, 2000
General Principles • Catching the bugs automatically
Checking Bugs -- Compiler • Turn on all warnings • gcc -Wall myfile.cpp while (n<total); { ... } if (a=1) ... while ((*p1++=*p2++)!=NULL) NULL;
Checking Bugs -- assert() • #include <assert.h> • #include <cassert> // for STL • Debug version • gcc myfile.cpp • Release version • gcc -DNDEBUG myfile.cpp
Example 1 of assert() Stack::pop() { assert(size>0); ... // function body }
Example 2 of assert() BOOL WaitingTree::HasDeadLock(int iClient, int *pPageNum, PIntSet pisWaitingFor) { // Verify parameters assert((iClient>=0) && (iClient<iNumClients)); assert(pPageNum!=NULL); assert(!pisWaitingFor->IsEmpty()); // Verify other constraints assert(!pisWaitingFor->Has(iClient)); ... // function body }
Checking Bugs -- by yourself • Check the consistency of the data structure while (get_event(e)) { handle_event(e); #ifndef NDEBUG check(); #endif }
Checking the data structure void check() { for (page in the memory-list) { assert(this page is in the hash table); assert(page->flag == IN_MEMORY); } for (page in the hash table) { assert(this page is in the memory-list); } }
Using Proper Data Structure • Vector • List • Set (Sorted List) • Heap • Hash table • Combination of basic structures
Example 1 -- LRU Algorithm • Operations: search, erase, insert • What data structure is preferable? • List supports erase, insert • Hash table supports search • The combination: • Pages are stored in the list • Pointers to pages are stored in the hash table
Example 2 -- LFU Algorithm • Operations: search, erase, insert, manage reference count • What data structure is preferable? • One list for pages with the same reference count • All lists are organized in an vector or a list • A hash table is used to locate each page
Config File [section1] item1 value1 item2 value2 value3 -1 ... [endsection1] [section2] ... [endsection2] %sim myconfig.cfg
Debugging by gdb • emacs+gdb is available everywhere • ddd -- a GUI wrapper for gdb, on skorpio, ultra*, and donald (see the screen snapshot)
Emacs + gdb • gdb Manual • http://www.cs.usask.ca/grads/wew036 • Compile • gcc -g myfile.cpp • Debug • gdb a.out
Gdb commands -- breakpoints • b [filename:]linenumber -- set breakpoint • c-x space-bar in emacs • info b -- list all breakpoints • d [n] -- delete breakpoint n
Gdb commands -- controlling the execution • run (r) -- run your program • next (n) -- run to the next source line • step (s) -- step to a different source line • finish -- run until the current function finish • continue (c) -- continue the running • backtrace (bt) -- print all frames in the stack
Gdb commands -- View variables • print (p) -- print the value of a variable • display -- automatically display variable • info display -- list all displays • undisplay [n] -- delete a display
Gdb commands -- View variables for STL • Never print or display a data in a STL structure: • p vInt[1] • disp *it • Assign them to a simple variable first • i=vInt[1]; // print i in gdb • ptr=&(*it); // disp *ptr in gdb
More details are aviable athttp://www.cs.usask.ca/grads/wew036/prog-tips Questions?