Path: utzoo!mnetor!uunet!lll-winken!lll-tis!ames!pacbell!att-ih!ihnp4!ihlpf!nevin1 From: nevin1@ihlpf.ATT.COM (00704a-Liber) Newsgroups: comp.lang.misc Subject: Var scoping in Wirth-type languages (was: Poor Algorithms) Message-ID: <3949@ihlpf.ATT.COM> Date: 10 Mar 88 01:51:49 GMT References: <3821@ihlpf.ATT.COM> <2791@enea.se> Reply-To: nevin1@ihlpf.UUCP (00704a-Liber,N.J.) Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 80 In article <2791@enea.se> sommar@enea.UUCP(Erland Sommarskog) writes: >Nevin J Liber (nevin1@ihlpf.UUCP) writes: >>How many novice Pascal programmers use many global vars because it saves >>memory? On a paging system, though, you may need more pages in real memory >>because of the way the chaining of variables is done in Pascal. > > Nevin would have loved them. They used a lot of global variables. >A typical procedure could have some local variables, but mostly they >used the used the global ones, even for typical local purposes. To >make it even more fun, all variables had one-letter prefixes like >cnumber, cix etc. And on top of all, the modules were *huge*. 6000 >lines or so. > >So moral: You may save some execution time by having your variables >global, but you since you are obscuring your code so fatally, >you will lose the notion of what you are doing and introduce >bugs and inefficiencies instead. I would NOT have loved it!! I hope that I didn't give the impression that I like lots of global variables; that is not what I meant to do. I happen to think that the scoping rules are a plus for Pascal/Modula-2; but there are other implementations that can be done when efficiency is necessary. Globals, unless implemented with the optimization I'm going to state later, are much less efficient than using locals! If I didn't make that point clear in my last posting, please excuse me. For every level deep that a variable is used after it is declared, one climb up the static chain of activation records (I think this is the right term; it's been a long time since I took an implementation of PLs class) is required to access the variable (this is due to the possibility of recursion). For example: procedure p1(); var a1,b1:integer; procedure p2(); var a2,b2:integer; procedure p3(); var a3,b3:integer; begin ... end begin ... end begin ... end I am defining proc p1() to be on level 1 and p2() on level 2 (making p3() a level 3 declaration). In order for p3() to access var a1, 2 climbs on the static chain are required. This is in contrast to p3() accessing a3, which require no climbing of the static chain. If some vars are declared 'globally' (at the topmost level), it is very inefficient to access those variables in a procedure declared more than 1 or 2 levels deep. There is a way to implement scoping that alleviates this inefficiency, however. In addition to the standard var declarations, one should be allowed to declare a variable 'static' (to steal from C). The scoping would be the same as for standard vars (ex: if var a2 in the above example were declared as static, then only proc p2() and proc p3() could access it), but it would always take up the same physical location in memory (it would not be part of the activation record). The only difference between a static var and a standard var is that when used recursively, the same static var is always manipulated (it would be like implicitly passing it as a 'var' type). Note: This optimization can already be done on variables that can be determined not to be used recursively and are referenced at a level deeper then they are declared. However, by adding explicit static declarations, the programmer has much more flexibility. Comments?? -- _ __ NEVIN J. LIBER ..!ihnp4!ihlpf!nevin1 (312) 510-6194 ' ) ) "The secret compartment of my ring I fill / / _ , __o ____ with an Underdog super-energy pill." / (_