1 / 19

Tips For Programming & Simulation

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;.

duer
Download Presentation

Tips For Programming & Simulation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tips For Programming & Simulation Wenguang Wang March 24, 2000

  2. General Principles • Catching the bugs automatically

  3. Checking Bugs -- Compiler • Turn on all warnings • gcc -Wall myfile.cpp while (n<total); { ... } if (a=1) ... while ((*p1++=*p2++)!=NULL) NULL;

  4. Checking Bugs -- assert() • #include <assert.h> • #include <cassert> // for STL • Debug version • gcc myfile.cpp • Release version • gcc -DNDEBUG myfile.cpp

  5. Example 1 of assert() Stack::pop() { assert(size>0); ... // function body }

  6. 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 }

  7. Checking Bugs -- by yourself • Check the consistency of the data structure while (get_event(e)) { handle_event(e); #ifndef NDEBUG check(); #endif }

  8. 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); } }

  9. Using Proper Data Structure • Vector • List • Set (Sorted List) • Heap • Hash table • Combination of basic structures

  10. 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

  11. 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

  12. Config File [section1] item1 value1 item2 value2 value3 -1 ... [endsection1] [section2] ... [endsection2] %sim myconfig.cfg

  13. Debugging by gdb • emacs+gdb is available everywhere • ddd -- a GUI wrapper for gdb, on skorpio, ultra*, and donald (see the screen snapshot)

  14. Emacs + gdb • gdb Manual • http://www.cs.usask.ca/grads/wew036 • Compile • gcc -g myfile.cpp • Debug • gdb a.out

  15. Gdb commands -- breakpoints • b [filename:]linenumber -- set breakpoint • c-x space-bar in emacs • info b -- list all breakpoints • d [n] -- delete breakpoint n

  16. 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

  17. 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

  18. 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

  19. More details are aviable athttp://www.cs.usask.ca/grads/wew036/prog-tips Questions?

More Related