DOC PREVIEW
UW CSE 303 - C Preprocessor

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

11/4/20091David Notkin  Autumn 2009  CSE303 Lecture 16#preprocessorDebugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.--Brian W. KernighanC preprocessor• Part of the C compilation process; recognizes special # statements,modifies your source code before it is compiled• This is generally considered to be a “macro processing” toolfunction description#include <filena me>insert a library file's contents into this file#include "filena me"insert a user file's contents into this file#define name [value]create a preprocessor symbol ("variable")#if testif statement#elseelse statement#elif testelse if statement#endifterminates an if or if/else statement#ifdef nameif statement; true if name is defined#ifndef nameif statement; true if name is not defined#undef namedeletes the given symbol nameConstant replacement• The preprocessor can be used to create constants:#define NUM_STUDENTS 100#define DAYS_PER_WEEK 7...double grades[NUM_STUDENTS];int six_weeks = DAYS_PER_WEEK * 6; // 42printf("Course over in %d days", six_weeks);• When the preprocessor runs before compilation, 7 is literally inserted into the code wherever DAYS_PER_WEEK is seen: DAYS_PER_WEEK does not exist in the eventual programint six_weeks = 7 * 6; // 42Optional debugging code#define DEBUG...#ifdef DEBUG// debug-only codeprintf("Size of stack = %d\n", stack_size);printf("Top of stack = %p\n", stack);#endifstack = stack->next; // normal code• How is this different from declaring a bool/int named DEBUG?Advanced definitions• #define can be used to modify the C language• (No different in mechanism than constants)#define AND &&#define EQUALS ==#define DEREF ->...Point p1 = (Point*) malloc(sizeof(Point));p1 DEREF x = 10;p1 DEREF y = 10;if (p1 DEREF x EQUALS p1 DEREF y AND p1 DEREF y > 0) {p1 DEREF x++;}Good idea? Bad idea? Neutral idea?Preprocessor macros• Similar to a function, but created inline before compilation#define SQUARED(x) x * x#define ODD(x) x % 2 != 0int a = 3;int b = SQUARED(a);if (ODD(b)) {printf("%d is an odd number.\n", b);}• The above literally converts the code to the following and compiles:int b = a * a;if (b % 2 != 0) { ...11/4/20092Subtleties• the preprocessor just replaces tokens with tokens#define foo 42int food = foo; // int food = 42; okint foo = foo + foo; // int 42 = 42 + 42; bad• preprocessor macros can do a few things functions cannot:#define NEW(t) (t*) calloc(1, sizeof(t))...Node* list = NEW(Node);Caution with macros• since macros are expanded directly, strange things can happen if you pass them complex values#define ODD(x) x % 2 != 0...if (ODD(1 + 1)) {printf("It is odd.\n"); // prints!}• The above converts the code to the followingif (1 + 1 % 2 != 0) {• Fix: Always surround macro parameters in parentheses.#define ODD(x) (x) % 2 != 0Running the preprocessor• to define a preprocessor variable, use the -D argument$ gcc -D DEBUG -o example example.c• to run only the preprocessor, use the -E argument to gcc$ gcc -E example.cint main(void) {if ((1 + 1) % 2 != 0) {printf("It is odd.\n");}return 0;}• rarely used in practice, but can be useful for debugging / learninggdb• gdb : GNU debugger. Helps you step through C programs.– absolutely essential for fixing crashes and bad pointer code– your program must have been compiled with the -g flag• usage$ gdb programGNU gdb Fedora (6.8-23.fc9)Copyright (C) 2008 Free Software Foundation, Inc...(gdb) run parameters...• redirecting input:$ gdb program(gdb) run parameters < inputfilegdb commandscommand descriptionrun or r parametersrun the programbreak or b placesets a breakpoint at the given place:- a function's name- a line number- a source file : line numberprint or p expressionprints the given value / variablestep or sadvances by one line of code("step into")next or nadvances by one line of code("step over")finishruns until end of function ("step out")continue or cresumes running programbacktrace or btdisplay current function call stackquit or qexits gdbA gdb session$ gdb intstackGNU gdb 5.2.1Copyright 2002 Free Software Foundation, Inc.(gdb) b 34Breakpoint 1 at 0x4010ea: file intstack.c, line 34.(gdb) rStarting program: /home/user/intstackBreakpoint 1, main () at intstack.c:3434 Node* oldFront = stack;(gdb) p stack$1 = (Node *) 0x4619c0(gdb) n35 printf("%d\n", stack->data);(gdb) n36 stack = stack->next;(gdb) n37 free(oldFront);(gdb) p stack$4 = (Node *) 0x462856(gdb) p oldFront$2 = (Node *) 0x4619c0(gdb) p *oldFront$3 = {data = 10, next = 0x462856}(gdb) cContinuing.11/4/20093ddd• ddd (Data Display Debugger): Graphical front-end for gdb– allows you to view the values of your variables, pointers, etc.– $ ddd programNamenemiver• nemiver : Another graphical debugger front-end– design goal: Be usable even if you don't know gdbcommands– $ nemiver programName argumentsOther debuggers• Eclipse CDT (C/C++ Development Toolkit)– create a new Managed Make C Project– right-click project name, choose Debug As, Local C/C++ Applicationvalgrind• valgrind : A memory-leak detector and debugging toolvalgrind programName arguments(1) push, (2) pop, (3) clear, or (0) quit? 2==3888== Conditional jump or move depends on uninitialised value(s)==3888== at 0x80484E7: main (intstack.c:28)==3888== ==3888== Use of uninitialised value of size 4==3888== at 0x80484F2: main (intstack.c:30)-15156339==3888== ==3888== Use of uninitialised value of size 4==3888== at 0x8048507: main (intstack.c:31)==3888== ==3888== Invalid free() / delete / delete[]==3888== at 0x4025DFA: free (vg_replace_malloc.c:323)==3888== by 0x8048517: main (intstack.c:32)==3888== Address 0x8048569 is in the Text segment of /home/stepp/intstackValgrind: leaks, too• stats about leaked memory on program exit(1) push, (2) pop, (3) clear, or (0) quit? 1Number to push? 10(1) push, (2) pop, (3) clear, or (0) quit? 1Number to push? 20(1) push, (2) pop, (3) clear, or (0) quit? 220(1) push, (2) pop, (3) clear, or (0) quit? 210(1) push, (2) pop, (3) clear, or (0) quit? 0==5162== LEAK SUMMARY:==5162== definitely lost: 16 bytes in 2 blocks.==5162== possibly lost: 0 bytes in 0 blocks.==5162== still reachable: 0 bytes in 0 blocks.==5162== suppressed: 0 bytes in 0


View Full Document

UW CSE 303 - C Preprocessor

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

22 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
Download C Preprocessor
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C Preprocessor and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C Preprocessor 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?