Docsity

Variables Initialization, Java and Name Collisions | CS 403, Study notes of Programming Languages

Toggle sidebar
bg1
1
Spring 2002 CS 403 Class Notes Page 1
Lecture 19 – Thursday, March 14
• Today
– Revisi t variable initialization discussion
– Java and name collisions
• Reading Assignments For Next Class
– Finish reading Chapter 6
• Assignments
– Assignment #4 due Tuesday (March 19th)
– Assignment #5 due Thursday (April 4th)
•Exams:
– Exam 2: Moved f rom March 21st to Tuesday, April 9th
Spring 2002 CS 403 Class Notes Page 2
Variable Initialization
• Three basic approaches:
1. Ignore uninitialized variables.
2. Initialize everything to zero (default value).
3. Give an error if something is used without a
previous initialization
Spring 2002 CS 403 Class Notes Page 3
Costs and Issues
• Strategy 1: Ignore uninitialized variables:
– Doesn’t cost anything
– Can result in really strange behavior
– A function with an uninitialized variable can exhibit
different behavior on different calls.
Spring 2002 CS 403 Class Notes Page 4
Example
void foo(void){
int x;
cout << x << endl;
}
void main(void){
ugh();
foo();
blah();
foo();
};
Spring 2002 CS 403 Class Notes Page 5
Costs and Issues (cont.)
• Strategy 2: Initialize all variables to default
– Static variables can be initialized at compile time
– This is done in C++, for example.
– Stack and heap variables incur run-time
initialization cost
Spring 2002 CS 403 Class Notes Page 6
Costs and Issues (cont.)
• Strategy 3: Give an error when an uninitialized
variable is used.
• Issue: A use of an uninitialized variable is
typically preceded by more than one execution
path.
pf3

Partial preview of the text

Download Variables Initialization, Java and Name Collisions | CS 403 and more Study notes Programming Languages in PDF only on Docsity!

Spring 2002 CS 403 Class Notes Page 1

Lecture 19 – Thursday, March 14

• Today

  • Revisit variable initialization discussion
  • Java and name collisions

• Reading Assignments For Next Class

  • Finish reading Chapter 6

• Assignments

  • Assignment #4 due Tuesday (March 19th^ )
  • Assignment #5 due Thursday (April 4th^ )

• Exams:

  • Exam 2: Moved from March 21 st^ to Tuesday, April 9th

Spring 2002 CS 403 Class Notes (^) Page 2

Variable Initialization

• Three basic approaches:

1. Ignore uninitialized variables.

2. Initialize everything to zero (default value).

3. Give an error if something is used without a

previous initialization

Spring 2002 CS 403 Class Notes (^) Page 3

Costs and Issues

• Strategy 1: Ignore uninitialized variables:

– Doesn’t cost anything

– Can result in really strange behavior

– A function with an uninitialized variable can exhibit

different behavior on different calls.

Spring 2002 CS 403 Class Notes Page 4

Example

void foo(void){

int x;

cout << x << endl;

void main(void){

ugh();

foo();

blah();

foo();

Spring 2002 CS 403 Class Notes (^) Page 5

Costs and Issues (cont.)

• Strategy 2: Initialize all variables to default

– Static variables can be initialized at compile time

– This is done in C++, for example.

– Stack and heap variables incur run-time

initialization cost

Spring 2002 CS 403 Class Notes (^) Page 6

Costs and Issues (cont.)

• Strategy 3: Give an error when an uninitialized

variable is used.

• Issue: A use of an uninitialized variable is

typically preceded by more than one execution

path.

Spring 2002 CS 403 Class Notes Page 7

Multiple Execution Paths

void main(){ int x, y, z; (1) cin >> x; (2) if x > 0 (3) y = 3; (4) else y = 4; (5) cout << y << endl; (6) }

  • {1, 2, 3, 4, 6} and {1, 2, 3, 5, 6} are both possible execution paths, depending on the input.

Spring 2002 CS 403 Class Notes (^) Page 8

Multiple Execution Paths

• Three cases:

1. All paths leading to a use contain a definition for

that use. (Definitely OK)

2. No path leading to a use contains a definition for

that use. (Definitely bad)

3. Some paths leading to a use contain a definition

for that use, while other paths do not. (This case

is harder to figure out).

Spring 2002 CS 403 Class Notes (^) Page 9

Case 1: All Paths OK.

void main(){ int x, y, z; (1) cin >> x; (2) if x > 0 (3) y = 3; (4) else y = 4; (5) cout << y << endl; (6) }

  • Execution paths: {1, 2, 3, 4, 6} and {1, 2, 3, 5, 6}.
  • Both statements (4) and (5) contain a definition of y.
  • Use of y in (6) is definitely preceded by a definition in either (4) or (5).
  • Strategy (3) should definitely not flag this case as defective.

Spring 2002 CS 403 Class Notes Page 10

Case 2: No Path OK.

void main(){ int x, y, z; (1) cin >> x; (2) if x > 0 (3) z = 3; (4) else z = 4; (5) cout << y << endl; (6) }

  • Execution paths: {1, 2, 3, 4, 6} and {1, 2, 3, 5, 6}.
  • Neither statement (4) nor (5) contains a definition of y.
  • Use of y in (6) is definitely not preceded by a definition.
  • Strategy (3) should definitely flag this case as defective, which can be done at compile-time.

Spring 2002 CS 403 Class Notes (^) Page 11

Case 3: Some paths ok, some not

void main(){ int x, y, z; (1) cin >> x; (2) if x > 0 (3) z = 3; (4) else y = 4; (5) cout << y << endl; (6) }

  • Execution paths: {1, 2, 3, 4, 6} and {1, 2, 3, 5, 6}.
  • Statement (5) contains a definition of y, but statement (4) does not.
  • Use of y in (6) may or may not be preceded by a definition.
  • It is feasible to flag this case as defective at compile-time, although the path {1, 2, 3, 4, 6} may not be executed on a given run of the program.

Spring 2002 CS 403 Class Notes (^) Page 12

Unexecutable Paths Cause Problems

void main(){ int x, y, z; (1) cin >> x; (2) if x > 0 (3) y = 3; (4) else z = 3; (5) if x > 0 (6) cout << y << endl; (7) }

  • Compiler should not flag this case, as the only anomalous

path is non-executable.

  • Compiler can’t decide which paths are executable.
  • Therefore, if the compiler flags the previous slide as an error it

must also flag this one, which is quite restrictive.