I did something similar with FORTRAN, putting an array 1 element long in
common area, then finding the address of it with, IIRC, ADR() intrinsic,
then subtracting that from the address I wanted to access, then using
that result as the subscript to access the array (no array bounds
checking) and getting whatever memory location I wanted. All locations
were within my program, I never tried to see if I could access memory
owned by the OS. Circa 1984 on VAX. Used so that the interpreter for my
DSL could access variables in the main program. Not exactly a union
type, but the same effect.
Bruce
On 31/10/2012 3:25 p.m., Cédric Beust ♔ wrote:
Bouncing off another topic mentioned in the latest podcast: union
types. Yes, C had them but the first time I was exposed to them was in
Pascal, and the discussion on the podcast reminded me of an awesome
hack that blew my mind a very long time ago.
Pascal was well known to be very strict and safe. Among other things,
it didn't let you access the memory directly, which was a big deal in
the 8 bit era where memory protection was a distant dream and PEEK and
POKE were how you wrote games.
And then, one day, somebody found a way to address the memory using
standard Pascal. Here is the trick (from memory, so it's probably not
quite correct):
type
b = record
x : array[1..65536] of ^integer;
y : integer;
end;
This declares a union type that is made of either an array of 65k
pointers to integers or a single integer. Then you initialize this
record in its "x" side with the memory address you want to peek and
you access it by using the "x" side of the record.
It took me months to understand what this code did, but what a
revelation it was when it finally clicked...
By the way, these types are formally known as "sum types" (Either is
one of them).
--
Cédric
--
You received this message because you are subscribed to the Google
Groups "Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.
--
You received this message because you are subscribed to the Google Groups "Java
Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.