Class Note for CS 403 at UA-Programming Languages (33)
Class Note for CS 403 at UA-Programming Languages (33)
Popular in Course
Popular in Department
This 16 page Class Notes was uploaded by an elite notetaker on Friday February 6, 2015. The Class Notes belongs to a course at University of Alabama - Tuscaloosa taught by a professor in Fall. Since its upload, it has received 17 views.
Reviews for Class Note for CS 403 at UA-Programming Languages (33)
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 02/06/15
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 21st to Tuesday April 9th Spring 2002 CS 403 Class Notes Page 1 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 2 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 3 Example void foovoid int x cout ltlt x ltlt endl void mainvoid UghO f00 blah f00 Spring 2002 CS 403 Class Notes Page 4 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 runtime initialization cost Spring 2002 CS 403 Class Notes Page 5 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 6 Multiple Execution Paths void main int x y z cin gtgt x ifx gt O y 3 else y 4 cout ltlt y ltlt endl 1 2 3 4 6 and 1 2 3 5 6 are both possible execution paths 1 2 3 4 5 6 depending on the input Spring 2002 CS 403 Class Notes Page 7 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 8 Case 1 All Paths OK void main int x y z 1 cin gtgt x 2 if x gt o 3 y 3 4 else y 4 5 cout ltlt y ltlt 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 9 Case 2 No Path OK void main int x y z 1 cin gtgt x 2 if x gt O 3 z 3 4 else 2 4 5 cout ltlt y ltlt 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 compiletime Spring 2002 CS 403 Class Notes Page 10 Case 3 Some paths ok some not void main int x y z 1 cin gtgt x 2 ifx gt O 3 z 3 4 else y 4 5 cout ltlt y ltlt 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 compiletime 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 11 Unexecutable Paths Cause Problems void main int x y z 1 cin gtgt x 2 if x gt O 3 4 cout ltlt y ltlt endl 7 Compiler should not flag this case as the only anomalous path is nonexecutable 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 Spring 2002 CS 403 Class Notes Page 12 Implementing Strategy 3 generating error messages Compiler can generate an error message When every execution path preceding the use of a variable fails to contain a definition this is ok but it s seldom this simple When some execution path preceding the use of a variable fails to contain a definition Java does this It s quite restrictive may give an error when no problem will actually occur in practice Compromise solution Generate errors in the easy every path case Generate warnings in the second case Optionally Employ runtime detection of variable usage errors This is extremely expensive and seldom practiced for compiled languages Typically requires using an extrabit flag Spring 2002 CS 403 Class Notes Page 13 Java and Name Conflicts It s harder to generate name conflicts in Java than I first thought Naive approach Class main uses class foo Two classes named foo appear in the same directory This doesn t work because of the requirement that compilation unit name class name Can t have two files of the same name in the same directory Spring 2002 CS 403 Class Notes Page 14 Second Attempt Directory A Class main and class foo main refers to foo Directory B Contains class foo Set the classpath to contain both A and B Problem foo is loaded from the first directory to appear in the classpath med fgo appearing in the second directory in the classpath is not even oa e Not a name conflict exactly but there is an ambiguity The foo that is loaded may not be the foo you intended depends on the precise order of the classpath This could result in subtle defects that might be hard to figure out Packages will allow you to load both foo s simultaneously although you will be required to do qualified naming Exception If you use import on the packages you can get a true naming conflict where the compiler will complain about a redeclaration Spring 2002 CS 403 Class Notes Page 15 What to do on the assignment Problem 6 An implementation in Java where there are two class foo s and the one that is loaded is not the one you intended and hence some kind of error results This must be done by putting the foo s in different directories and explicitly defining the classpath Don t forget that you can define the classpath with the classpath switch in the compiler command line you don t have to change the machine path variables Problem 7 Put the 2 foo s in packages and show how the above problem goes away Spring 2002 CS 403 Class Notes Page 16
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'