Class Note for CS 403 at UA-Programming Languages (8)
Class Note for CS 403 at UA-Programming Languages (8)
Popular in Course
Popular in Department
This 3 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 15 views.
Reviews for Class Note for CS 403 at UA-Programming Languages (8)
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 19 Assignment 5 due Thursday April 4 Exams Exam 2 Moved from March 21St to Tuesday April 9 smznnz cs 402 Notes P 1 Example void foovoid int x cout ltlt x ltlt endl I void mainvoid UQhO f000 blah foo I smznnz cs 402Nm P e 4 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 smznnz cs 402 Notes P 2 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 smznnz cs 402Nm P e 5 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 smznnz cs 402 Notes P 2 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 smznnz cs 402Nm P e 5 Multiple Execution Paths yeiid main irftx y 2 sin gtgt x fxgtu y 3 else y 4 eeiutltlt yltlt endl e l 9E3 1 2 3 4 6 and l 2 3 5 6 are potn possiple execution paths depending ontne input smzuuz cs AUZChssNaies P 27 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 smzuuz cs AUZChssNaies P e2 Case 1 All Paths OK yeiid main irftx y z i sin gtgt x 2 ifx gt u a V 3i 4 else y 4 5 eeiutltlt yltlt endl e Execution patns 42 3 4 6 and l 2 3 5 6 Botn statements 4 and 5 contain a definition ofy Use ofy in 6 is definitely preceded by a definition in eitner4 or 5 Strategy 3 snould definitelynotflag tnis case as defectiye smzuuz cs AUZChssNaies P 29 Case 2 No Path OK yuid main mxyz ein gtgt x fxgtu z 3 else 1 4 5 eeiutltltyltlt endl e l 2 a 4 Execution patns l 2 34 6arldl2356l Nelther statement 4 nor5 contains a definition ofy Use of y in 6 is definitely not preceded by a definition Strategy 3 snould definitely flag tnis case as defectiye wnicn can be done at compllentlme smzuuz cs AUZClassNatzs P e m Case 3 Some paths ok some not yuid main intx yz ein gtgt x fxgtu z 3 else y 4 EDUI ltltyltlt endl e l HQ QES Execution patns 12346larld12356 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 lt is feasible to flag tnis case as defectiye at compileetime altnougn tne patn l 2 3 4 6 may not be executed on a giyen run of tne program smzuuz cs AUZClassNatzs P 2 ll Unexecutable Paths Cause Problems yeiid mainf int x y z 4 cm gtgt x 2 if x gt u a y 3i 4 else 1 a 5 if x gt u a cuutltltyltlt endl 7 l Compiler should not ag this case as the only anomalous path is nonexecutable Compiler can t decide which paths are executable Therefore if the compiler ags the previous slide as an error it must also ag this one which is quite restrictive smzuuz cs AUZClassNatzs P 212 Implementing Strategy 3 generating error messages Compiler can generate an error message 7 When eyery execution path precedlhg the use or a yarlaole ralls to cohtalh a derlhltloh thls ls oh but lt s seldom thls slrhple 7 When some execution path precedlhg the use or a yarlaole ralls to cohtalh a derlhltloh Java doesthls t s dulte restrletlye 7 may glye ah errurvyhen no problem Wlll actually oeeur lh practlce Compromise solution 7 Generate errors lh the easy eyery path case 7 Generate warhlhgs lh the second case r Optlohally Employ ruhetlrhe detectloh ofyal39lable usage errors Thls ls extremely expensive and seldom praetleed ror eorhplled languages Typlcally redulres uslhg ah extraebltflag smznnz cs AazcussNotes P 213 Java and Name Conflicts It s harderto generate name conflicts in Java than Ifirst 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 les of the same name in the same directory smznnz cs AazcussNotes P 214 Second Attempt Dlrectory A 7 class rhalhl and class fuu 7 man rerers to fun Dlrectory B e cohtalhs class fuu Set the classpath to cohtalh both A and B Problem 7 fun ls loaded rrorh the rlrst dlreetory to appearlh the elasspath e The fun appearlhg h the seeohd dlreetory h the elasspath ls hot eyeh loaded Not a name cohrllct exactly but there ls ah arholgulty r The fun that ls luaded may not be the fun you intended 7 depends on the preelse order or the elasspath r Thls could result ln subtle defects that rnlght be hard to gure out Packages Will allow you to load both foo s slrnultaneously although you Will be requlreo to do quallrleo harhlhg e EXEEptan lf you use lrhport oh the packages you can geta true harhlhg eunfllctvvhere the eorhplleryylll eorhplalh about a redeelaratloh smznnz cs AazcussNotes P 215 What to do on the assignment Problem 6 An implementation in Java where there are two class fooquots 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 de ning the classpath Don t forget that you can de ne 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 smznnz cs AazcussNoes P 216
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'