Presentation is loading. Please wait.

Presentation is loading. Please wait.

C. M. Overstreet Old Dominion University Fall 2005

Similar presentations


Presentation on theme: "C. M. Overstreet Old Dominion University Fall 2005"— Presentation transcript:

1 C. M. Overstreet Old Dominion University Fall 2005
CS 110: Code Analysis C. M. Overstreet Old Dominion University Fall 2005

2 Overview Some motivation for code analysis
Some examples: some easy, some hard, some impossible Some examples of where used 1/10/05

3 Example 1: elimination of infinite loops
Infinite loops are usually undesirable. But not always. Why? Can you write a program that checks another program for infinite loops? That works all of the time? Initial idea: just consider while loops: Check loop body to see if any action modifies some loop control variables. Does this work? If no change, is loop infinite? Can one always do this? How? What about input data? What about for loops? 1/10/05

4 Example 1 (cont.) Point: often partial solutions are useful even if a general solution does not exist. Question: What is the Halting Problem and why is it important? 1/10/05

5 Static & Dynamic Analysis
From their beginning, compilers have tried to optimize code. Done by primarily by static analysis Figure things out by analyzing source code Many types of useful information about code itself can only be obtained by running to code and monitoring its behaviors. This is dynamic analysis 1/10/05

6 Possible Common Compiler Optimizations
Flag unused variables Very common Make loops more efficient by moving code around Eliminating code that cannot affect program output Not done; too hard! Worked on a project for Navy a few years ago in which program manager suspected ~80% of code was useless About 1,000,000 lines of source code 1/10/05

7 Examples of analysis tools
gcov: Tool to identify which statements have been executed while testing a program gprof: Tool to tell a programmer which parts of a program use the most execution time. Useful when improving program speed. What are memory leaks? 1/10/05

8 Example 3: uninitialized variables
Uninitialized variables are usually undesirable. Should compilers make them illegal? Can compilers make them illegal? Can you write a program which detects uninitialized variables? Always? Sometimes? Using static analysis? i.e., uninitialized vars detected at compile time Using dynamic analysis? i.e. uninitialized vars detected at run time 1/10/05

9 Another example: side effects of function calls
Consider the statement: x = f(a,b) + a + g(a,c); Considered to be a dangerous statement. "Careful" programmers avoid this type of coding. Why? 1/10/05

10 Weiser: Program Slices
slice criterion: <13, {z}> 1, 2, 3, 6, 8, 9, 11, 13 slice criterion: <10, {x}> 1, 2, 3, 13 slice criterion: <13, {total}> 1, 2, 3, 4, 6, 8, 10, 11, 13 1 void main() 2 { int x, y, total, sum, z; 3 cin >> x >> y; 4 total = 0.0; 5 sum = 0.0; 6 if ( x < 1 ) 7 sum = y; 8 else { 9 cin >> z; 10 total = x*y; 11 } 12 cout << total << sum << endl; 13 } 1/10/05


Download ppt "C. M. Overstreet Old Dominion University Fall 2005"

Similar presentations


Ads by Google