[M3devel] function pointers and comparison to nil? mis-typed function pointers?

hendrik at topoi.pooq.com hendrik at topoi.pooq.com
Sat May 31 15:29:24 CEST 2008


On Fri, May 30, 2008 at 06:17:02PM -0500, Rodney M. Bates wrote:
> 
> My one handy Algol68 book has the usual tutorial language book problem: it
> omits the cases you really need to look up.  It only states that there will
> be problems if you use a dangling environment, but not whether the language
> specifies this should be detected by the language, or whether "all hell will
> break loose."  I'm guessing it's the latter. The implementation technique
> Hendrik describes makes it a detected runtime error, but not unless/until
> you try to use the lost environment.  This is more generous than Modula-3's 
> rule.

The actual Algol 68 definition does pronounce on this.  The CDC 
implementation was much more liberal than the language definition.

Every reference/variable and every procedure has a scope.  The scope of 
a variable is the level on the run-time stack at which it is allocated.  
Variables can be on the heap; this is global scope.  The scope of a 
procedure is the stack elvel at which its most local global identifier 
is bound.  Even if the identifier refers to an object on the heap, it is 
the level at which it is bound that counts.

There is a universal scope restriction:  No object can refer to a more 
local object.  The constraint in the language definition is applied on 
assignment.

I know of no Algol 68 implementation that I can say for sure implements 
this restriction with a run-time check.  Of course it can be done, 
either by tagging each pointer with an explicit mention of its stack 
level (which takes space) or by comparing its value and comparing it to 
various stack locations in a kind of search.

The first release of the CDC algol 68 compiler just allocated all 
variables on the heap, making the check unnecessary for safety.  
Programmers almost never wrote code that passed procedures out-of-scope.

Later releases performed static analysis to determine where it was safe 
to allocate in the stack (almost all the time), and used the mechanism I 
described earlier to check procedures when they were called if the 
static check didn't suffice..
> 
> And, of course, if your language is really dynamic, it could just say an
> environment is always accessible, requiring many or all activation records
> to be heap allocated.

Which was a proposal made for the original Algol 68, but was turned 
down.  I think it should have been accepted.  We would have had a 
strongly-typed Scheme ahead of its time.

-- hendrik




More information about the M3devel mailing list