|
Re: I think a compiler/language tutorial and a memory content exercise help
|
Posted: Dec 17, 2004 7:28 AM
|
|
In discusions involving language tools, the phrases "syntactic structure" and "semantic meaning" come up regularly. When you mix in "machine representation", you start to elevate all the visible issues that I think are important to understanding 'type'.
An example, bad C language program to demonstrate how the machine can be confused by 'machine representation' when the semantics of the type are not controlled by the languages syntactic structures.
void main( int argc, char args[][] ) {
char arr1[20];
int v1;
char arr2[20];
int v2;
char arr3[20];
int v3;
v1 = -1;
v2 = -2;
v3 = -3;
// copy in a 20 byte string
strcpy(arr1, "0123456789012345678");
// copy in 20 bytes
memcpy(arr2, "01234567890123456789", 20);
// mistakenly copy in a 21 byte string
strcpy(arr3, "01234567890123456789");
printf ("arr1: %s, arr2: %20.20s, arr3: %s, v1: %d, v2: %d\n", arr1, arr2, arr3, v1, v2, v3 );
}
You might also then include the same example in another language, such as Java and show the difference in what happens at runtime with type semantics.
public class x {
public static void main( String args[] ) {
byte arr1[] = new byte[20];
int v1;
byte arr2[] = new byte[20];
int v2;
byte arr3[] = new byte[20];
int v3;
v1 = -1;
v2 = -2;
v3 = -3;
// create a 19 byte array
byte[] b = "01234567890123456789".getBytes();
// create a 20 byte array
byte[] b2 = "01234567890123456789".getBytes();
// create a 21 byte array
byte[] b3 = "012345678901234567890".getBytes();
// copy in a 20 byte string
System.arraycopy(arr1, 0, b, 0, b.length );
// copy in 20 bytes
System.arraycopy(arr2, 0, b2, 0, b2.length );
// Try to copy in a 21 byte string
System.arraycopy(arr3, 0, b3, 0, b3.length );
System.out.println("arr1: "+arr1+
", arr2: "+arr2+
", arr3: "+arr3+
", v1: "+v1+
", v2: "+v2+
", v3: "+v3+
"" );
}
}
Type safety is one aspect of types that should allow students to understand why certain languages are more useful in particular circumstances, than others.
You could use some shell functions to show how non-typed protocols that are just named function. Create a shell script such as
#!/bin/bash
. $1
# do something here with a function in the file
# provided on the command line
...
This will show some elements of all the things that type conveys in various languages. The everything's a string flexibility of the shell and other similar scripting languages can be highly exploited. Used correctly, it can be quite impowering. Exploited for the sake of laziness, it can be disasterous.
If I really had to say a sentence or so about what type means, and provide no other explanation, ever, for a particular crowd, I think I'd say something like the following.
A type is like a human being. It has an outward appearence that is recognizable by visible attributes, but it also has an inner content and behavior which may not be understood without extensive investigation. Humans typecast because that groups and simplifies how many differences we really have to distinguish.
In computer programs, types (or typecasts) are used in similar ways. Somethings such as gender transend time (unless altered by a system change), while body shape, hair and eyesight vary over time, and don't necessarily change how a person interacts with the world.
Computer system types have similar life cycles because these systems do change over time. They are adapted to new uses and extended for new behaviours. Humans go to school to be trained for new behaviours. A computer program doesn't loose its training because it is a program. A Human might neglect its training through oversight or through distraction.
The misrepresentation of computer types by a language implementation can cause software to misbehave if distracting software is injected into the system. Viruses are a form of distracting software. Also, a software system can be misconfigured by an ignorant person or poorly written automation software and this can cause it to create incorrect results. HTML page syntax errors can be exploited to cause nondefensive programs to be compromized.
You could of course go on and on with similar ties between human type casts and computer types to show how many things are tied into this basic human need for compartmentalization and simplification.
Gregg
|
|