Scoping and TestingOverview of Today’s LectureGlobal VariablesDefinition vs. DeclarationLocal Variables and ParametersLocal Variables & ParametersSlide 7Scope ExampleScope: Another ExampleScope: AScope: BScope: CScope: DScope: EScope: Keeping it SimpleScope and Programming StyleTestingSlide 18Testing, Profiling, & InstrumentationProgram VerificationProgram TestingTest ProgramsSlide 23Test Boundary ConditionsSlide 25Test Boundary ConditionA Bit Better...Ambiguity in SpecificationMoral of This Little Story:Test As You Write CodeTest AutomationStress TestsWho Tests WhatConclusions1Scoping and TestingProfessor Jennifer RexfordCOS 2172Overview of Today’s Lecture•Scoping of variablesLocal or automatic variablesGlobal or external variablesWhere variables are visible•Testing of programsIdentifying boundary conditionsDebugging the code and retesting3Global Variables•Functions can use global variables defined outside and above themint stack[100];int main(void) { . . . stack is in scope}int sp;void push(int x) { . . . stack, sp are in scope}4Definition vs. Declaration•DefinitionWhere a variable is created and assigned storage•DeclarationWhere the nature of a variable is stated, but no storage allocated•Global variablesDefined once (e.g., “int stack[100]”)Declared where needed (e.g., “extern int stack[]”)–Only needed if the function does not appear after the definition–Convention is to define global variables at the start of the file5Local Variables and Parameters•Functions can define local variables Created upon entry to the functionDestroyed upon departure and value not retained across calls–Exception: “static” storage class (see chapter 4 of K&R)•Function parameters behave like initialized local variablesValues copied into “local variables”C is pass by value (so must use pointers to do “pass by reference”)6Local Variables & Parameters•Function parameters and local definitions“hide” outer-level definitions (gcc -Wshadow)int x, y;. . .void f(int x, int a) { int b; . . . y = x + a * b; if (. . .) { int a; . . . y = x + a * b; }}different xsame ydifferent a7Local Variables & Parameters•Cannot declare the same variable twice in one scopevoid f(int x) { int x; error! . . . }8Scope Exampleint a, b;void f(int a) { a = 3; { int b = 4; printf(“a=%d, b=%d\n”, a, b); } printf(“a=%d, b=%d\n”, a, b); b = 5;}int main (void) { a = 1; b = 2; f(a); printf(“a=%d, b=%d\n”, a, b); return 0;}9Scope: Another Example#include “interface.h”int A;int B;void f(int C) { int D; if (...) { int E; ... }}void g(...) { int H; ...}#include “interface.h”int J;void m(...) { int K; ...}void g(...) { int H; ...}extern int A;void f(int C);module1.c module2.cinterface.h10Scope: A#include “interface.h”int A;int B;void f(int C) { int D; if (...) { int E; ... }}void g(...) { int H; ...}#include “interface.h”int J;void m(...) { int K; ...}void g(...) { int H; ...}extern int A;void f(int C);module1.c module2.cinterface.h11Scope: B#include “interface.h”int A;int B;void f(int C) { int D; if (...) { int E; ... }}void g(...) { int H; ...}#include “interface.h”int J;void m(...) { int K; ...}void g(...) { int H; ...}extern int A;void f(int C);module1.c module2.cinterface.h12Scope: C#include “interface.h”int A;int B;void f(int C) { int D; if (...) { int E; ... }}void g(...) { int H; ...}#include “interface.h”int J;void m(...) { int K; ...}void g(...) { int H; ...}extern int A;void f(int C);module1.c module2.cinterface.h13Scope: D#include “interface.h”int A;int B;void f(int C) { int D; if (...) { int E; ... }}void g(...) { int H; ...}#include “interface.h”int J;void m(...) { int K; ...}void g(...) { int H; ...}extern int A;void f(int C);module1.c module2.cinterface.h14Scope: E#include “interface.h”int A;int B;void f(int C) { int D; if (...) { int E; ... }}void g(...) { int H; ...}#include “interface.h”int J;void m(...) { int K; ...}void g(...) { int H; ...}extern int A;void f(int C);module1.c module2.cinterface.h15Scope: Keeping it Simple•Avoid duplicate variable namesDon’t give a global and a local variable the same nameBut, duplicating local variables across different functions is okay–E.g., array index of i in many functions•Avoid narrow scopesAvoid defining scope within just a portion of a function–Even though this reduces the storage demands somewhat•Use narrow scopes judiciouslyAvoid re-defining same/close names in narrow scopes•Define global variables at the start of the fileMakes them visible to all functions in the fileThough, avoiding global variables whenever possible is useful16Scope and Programming Style•Avoid using same names for different purposesUse different naming conventions for globals and localsAvoid changing function argumentsBut, duplicating local variables across different functions is okay–E.g., array index of i in many functions•Define global variables at the start of the fileMakes them visible to all functions in the file•Use function parameters rather than global variablesAvoids misunderstood dependenciesEnables well-documented module interfaces •Declare variables in smallest scope possibleAllows other programmers to find declarations more easilyMinimizes dependencies between different sections of code17TestingChapter 6 of “The Practice of Programming”18"On two occasions I have been asked [by members of Parliament!], `Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question." -- Charles Babbage19Testing, Profiling, & Instrumentation•How do you know if your program is correct?Will it ever crash?Does it ever produce the wrong answer?How: testing, testing, testing, testing, …•How do you know if your program is efficient?How fast is your program?Why is it slow for one input but not for another?How much memory is it using?How: timing, profiling, and instrumentation (later in the course)20Program Verification•How do you know if your program is correct? Can you prove that it is correct?Can you prove properties of
View Full Document