Lecture 3
Biostatistics And Medical Informatics 615 with Abecasis at University of Michigan - Ann Arbor
About this note
By: Anonymous
Textbook:
Algorithms in C, Parts 1-5 (Bundle): Fundamentals, Data Structures, Sorting, Searching, and Graph Algorithms (3rd Edition)
Numerical Recipes in C: The Art of Scientific Computing
Created: 2008-06-08
File Size: 51 page(s)
Views: 0
Textbook:
Algorithms in C, Parts 1-5 (Bundle): Fundamentals, Data Structures, Sorting, Searching, and Graph Algorithms (3rd Edition)
Numerical Recipes in C: The Art of Scientific ComputingCreated: 2008-06-08
File Size: 51 page(s)
Views: 0
About StudyBlue
STUDYBLUE makes things that make you better at school.
Things like online flashcards with photos and audio.
Things like personalized quizzes and friendly reminders about when (and what) to study next.
Think of it as a digital backpack™: access to all of your study materials online and on your phone.
STUDYBLUE exists to make studying efficient and effective for every student, for free. Join us.
“I have been getting MUCH better grades on all my tests for school. Flash cards, notes, and quizzes are great on here. Thanks!”
Kathy
Kathy
Sign up (free) to study this.
Programming in C: How to Get Things Done! Jan Wigginton Biostatistics 615/815 Last Lecture z Anatomy of a C program ? A collection of short functions z Built-in data types available in C z The C standard library Executing C Code z C is a high level language ? Relatively easy to understand z Computer CPUs execute much more detailed, "lower-level" instructions z A compiler performs the necessary translation? Working in a UNIX Environment z GCC ? Compile code z GDB ? Debug and test code z GPROF ? Collect profiling information GCC z GCC is a free C compiler ? GNU C Compiler ? G++ is a companion tool for C++ z Versions available for ? Linux ? Unix ? Mac ? Windows z Developed by Free Software Foundation Simple Usage gcc my_program.c ?o my_program -lm z Uses text file with C/C++ code as input z Produces executable program as output Compiling with multiple source files z All ?.cpp? files in current directory gcc *.cpp ?o my_program ?lm z All ?.cpp? files in ?my_directory? gcc ?Imy_directory my_program.cpp my_directory/*.cpp ?o my_program ?lm A shortcut z On Unix, it can be a time saver to create a ?build.csh? script #!/bin/tcsh gcc *.c ?o my_program ?lm z Make it executable chmod +x build.csh z To compile ./build.csh Programming errors z Compile time ? Program does not compile. ? Compiler reports a ?best guess? of the problem z Run time ? Executable crashes or has unexpected behavior ? May not appear for all conditions or all data sets Compiler output z Errors or warnings z Executable isn?t created until all errors are corrected z Program will still compile if warnings are not dealt with z Good practice to correct compiler warnings as well Common compiler errors z Undeclared variables or functions z Missing semicolon or brace z Typos z Missing files or libraries z Type ambiguities Common compiler errors Undeclared variable #include int main(int argc, char * argv[]) { printf("I am a computer program\n"); printf(?Value for variable %d", var); return 0; } Compiler message ./build.csh basic.cpp: In function `int main(int, char**)': basic.cpp:14: error: `var' undeclared (first use this function) basic.cpp:14: error: (Each undeclared identifier is reported only once for each function it appears in.) Common compiler errors z Missing semicolon #include void print_something() int main(int argc, char * argv[]) { printf("I am a computer program\n"); print_something(); return 0; } void print_something() { printf("Printing from a subroutine\n\n"); } z Compiler message ./build.csh print_something.cpp: In function `void print_something()': print_something.cpp:5: error: parse error before `int' print_something.cpp: In function `void print_something()': print_something.cpp:17: error: redefinition of `void print_something()' print_something.cpp:5: error: `void print_something()' previously defined here print_something.cpp:17: error: redefinition of `void print_something()' print_something.cpp:5: error: `void print_something()' previously defined here Common compiler errors z Missing brace #include void print_something(); void print_something_else(); int main(int argc, char * argv[]) { printf("I am a computer program\n"); return 0; } void print_something() { printf("Printing from a subroutine\n"); // } void print_something_else() { printf("Printing from subroutine II\n"); } z Compiler message ./build.csh basic.cpp: In function `void print_something()': basic.cpp:18: error: parse error before `{' token Common compiler errors z Missing library //include void print_something(); int main(int argc, char * argv[]) { printf("I am a computer program\n"); return 0; } void print_something() { printf("Printing from a subroutine\n"); } z Compiler message ./build.csh basic.cpp: In function `int main(int, char**)': basic.cpp:7: error: `printf' undeclared (first use this function) basic.cpp:7: error: (Each undeclared identifier is reported only once for each function it appears in.) basic.cpp: In function `void print_something()': basic.cpp:14: error: `printf' undeclared (first use this function) Common compiler errors z Type ambiguity #include #include int main(int argc, const char *argv[]) { printf("I am a computer program"); int var = log(10); return 0; } z Compiler message Visual C++ c:\Documents and Settings\Visual tudioProjects\Example\MyApplication.cp p (9): error C2668: 'log' : ambiguous call to overloaded function GCC ./build.csh overload.cpp: In function `int main(int, const char**)': overload.cpp:9: warning: converting to `int' from `double' Tips for working through compiler errors z Start at the top z Compiler may be ?confused? by missing semicolons or braces and produce many error messages that don?t seem logical. z Recompile as you work out errors z Check for missing header file declarations and code libraries Common run-time errors z Uninitialized variables z Memory errors z Numeric errors z Type errors in print statements z Closing a NULL file pointer z Accessing a NULL pointer z Variables out of scope Tips for tracking down run-time errors z Use a debugger z Debug data not code z Isolate a small case z Understand what?s happening before making changes z For updated code with new run-time errors ? Suspect recent changes ? Can also be a pre-existing bug that was uncovered by code changes Avoiding errors z Declare and initialize new variables as you code z Write paired coding constructs at the same time ? Open paren/ close paren ? Allocate/deallocate ? Constructor / destructor ? fopen/fclose z Always consider end cases z Pay attention to variable scope z Use error check code GDB z A simple debugger z Helps test and evaluate programs by: ? Stopping program at specific points ? Running program one line at a time ? Displaying the values of specific variables Simple Usage z Compile program with debug information gcc ?pg my_program.c ?o my_program -lm z Load program into debugger gdb my_program z Use debugger commands to control execution of program ? Essential GDB Commands z run ? Start execution of the program z next ? Execute a single line of code z step ? Execute a single line of code, stop at entry point of any called functions z continue ? Execute program until next breakpoint z where ? Reports line of code where crash occurs More Essential GDB Commands z break main ? Stop program when main function is called z break n ? Stop program when line n is reached z print x ? Print contents of variable x z info locals ? Print all local variables (gdb) break 1 Breakpoint 1 at 0x8048444: file example2.cpp, line 1. (gdb) list 2 3 #define UPPER 10 4 5 int vals[UPPER]; 6 7 void print_something(); 8 9 int main(int argc, const char * argv[]) 10 { 11 printf("I am a computer program\n"); (gdb) run Starting program: /home/wiggie/powerpoint/biostat851/example Breakpoint 1, main (argc=134513732, argv=0x1) at example2.cpp:10 10 { (gdb) n 11 printf("I am a computer program\n"); (gdb) n I am a computer program 13 for (int i = 0; i < UPPER; i++) (gdb) n 15 vals[i] = i + 1; . (gdb) info locals i = 0 (gdb) print vals[i] $1 = 0 (gdb) s 13 for (int i = 0; i < UPPER; i++) (gdb) print vals[i] $2 = 1 (gdb) s 15 vals[i] = i + 1; (gdb) break print_something() Breakpoint 2 at 0x8048496: file example2.cpp, line 24. (gdb) cont Continuing. Breakpoint 2, print_something () at example2.cpp:24 24 int var = 5, count; (gdb) list 19 return 0; 20 } 21 22 void print_something() 23 { 24 int var = 5, count; 25 printf("\n\nPrinting from a subroutine\n"); 26 var ++; 27 } 28 (gdb) n 25 printf("\n\nPrinting from a subroutine\n"); (gdb) print count $3 = -1076624204 (gdb) print var var A syntax error in expression, near `var'. (gdb) print var $4 = 5 (gdb) n Printing from a subroutine 26 var ++; (gdb) n Debugging with printf int printf(char * format, ?); z Writes formatted output z Help localize problems, particularly for tight loops or when array variables are suspect z Output is buffered, so printf should be followed call to fflush(stdout) printf int printf(char * format, ?); z Format string controls how arguments are converted to text z Parameters are printed as specified in % fields z %[flags][width][precision]type z Otherwise, string is quoted printf fields %[flags][width][precision]type Flags: z -? to left justify result ? ?+? to show sign in positive numbers Width z Minimum number of characters to print Precision z Number of digits after decimal (for floating point) ? Maximum number of characters (for strings) Type z ?s? for strings z ?d? for integers, ?x? to print hexadecimal integers ? z ?f? for floating point, ?e? for exponential notation, ?g? for automatic About compiler flags z Running GDB on executable that isn?t compiled with debug flags, will produce confusing output (gdb) run Starting program: /home/wiggie/powerpoint/biostat851/basic Breakpoint 1, 0x0804844a in main () (gdb) list 1 in ../sysdeps/i386/elf/start.S (gdb) print The history is empty. . (gdb) info locals No symbol table info available. z Recompile finished code without debug flags z Optimization flags (-O2 ?O3) can make executable faster z -Wall for ?picky? compilation GPROF z Works with GCC to collect information about a program's execution ? How often is each function called? ? How much time is spent in each function? z Works in three steps ? Compile code ? Execute program ? Tabulate profile information Simple Usage z Compile program with profile information gcc ?pg my_program.c ?o my_program -lm z Execute program ./my_program z Summarize profile information gprof my_program time fantasia:~/powerpoint/biostat851> time ./example I am a computer program I can count 1 potato 2 potato 3 potato 4 potato 5 potato 6 potato 7 potato 8 potato 9 potato 10 potato Printing from a subroutine 0.004u 0.000s 0:00.02 0.0% 0+0k 0+0io 0pf+0w fantasia:~/powerpoint/biostat851> Getting Help z From the UNIX prompt, you can use the man command to get help ? z For example: ?man gcc - info on how to run gcc ?man printf - info on using printf() Working in a Windows Environment z Good integrated toolsets exist z Good options include: ? Microsoft Visual Studio / Visual C++ ? Discounted version available through the University ? Turbo C++ Explorer ? Free C/C++ compiler, www.turboexplorer.com Microsoft Visual Studio z ?Student Tools for Visual Studio? ? Included in Academic Edition ? Simplifies environment slightly z Each application you develop is a project ? Can include a collection of source files ? But only one for most of our examples z Many types of applications possible ? ? Our examples will be ?Console Applications? ?All-In-One? z Edit and manage source files z Compile code z Run, debug and test File | New | Project ? Project | Add New Item | C++ File F1: Online Help System z Visual Studio includes an extensive integrated help system z Using the F1 key, you can get info on standard library functions: ? Including examples of how the function should be used. Important Commands z Build | Build MyApplication ? Compiles code, identifies problems with source z Debug | Start, Step Into, Step Over ? Manage execution of program z Debug | QuickWatch ? Examine contents of variables as program runs z Click to the left of particular source lines to set breakpoints Build | Build MyProject.cpp Debug | Start Debug | Start Stepping through code Adding a watch variable Call stack Today z How to compile, run and debug C code z GNU tool collection ? Unix / Linux / newer Macs z Integrated environments, such as Visual Studio ? Windows Questions/Clarification? z Feel free to drop me an e-mail : Janis Wigginton wiggie@umich.edu Goncalo Abecasis Microsoft PowerPoint - 615.03 -- Jan's Programming Tools
Back
Next
About this note
By: Anonymous
Textbook:
Algorithms in C, Parts 1-5 (Bundle): Fundamentals, Data Structures, Sorting, Searching, and Graph Algorithms (3rd Edition)
Numerical Recipes in C: The Art of Scientific Computing
Created: 2008-06-08
File Size: 51 page(s)
Views: 0
Textbook:
Algorithms in C, Parts 1-5 (Bundle): Fundamentals, Data Structures, Sorting, Searching, and Graph Algorithms (3rd Edition)
Numerical Recipes in C: The Art of Scientific ComputingCreated: 2008-06-08
File Size: 51 page(s)
Views: 0
About StudyBlue
STUDYBLUE makes things that make you better at school.
Things like online flashcards with photos and audio.
Things like personalized quizzes and friendly reminders about when (and what) to study next.
Think of it as a digital backpack™: access to all of your study materials online and on your phone.
STUDYBLUE exists to make studying efficient and effective for every student, for free. Join us.
“I have been getting MUCH better grades on all my tests for school. Flash cards, notes, and quizzes are great on here. Thanks!”
Kathy
Kathy