Comparison of Pascal and C - Type Escapes

Type Escapes

C supports the ability to "cast" any type to another type. While some imply a conversion of some sort (truncation, promotion etc.), pointer casts allow the user to simply assume the desired type is pointed:

int *a; float b; a = (int*) &b;

The meaning of such casts is entirely machine dependent. This feature often helps with low level conversion of data. For example, a floating point value can be output to a file as a series of bytes.

It may be possible to do this in Pascal using an undiscriminated variant record:

var a: integer; b: real; a2c: record case boolean of false: (a: integer); true: (b: real); end; end; begin a2c.b := b; a := a2c.a; end;

Although casting is possible on the most of Pascal compilers and interpreters, even in the code above a2c.a and a2c.b aren't required by any Pascal standardizations to share the same address space. Niklaus Wirth, the designer of Pascal, has written about the problematic nature of attempting type escapes using this approach:

"Most implementors of Pascal decided that this checking would be too expensive, enlarging code and deteriorating program efficiency. As a consequence, the variant record became a favourite feature to breach the type system by all programmers in love with tricks, which usually turn into pitfalls and calamities".

Several languages now specifically exclude such type escapes, for example Java, C# and Wirth's own Oberon.

Read more about this topic:  Comparison Of Pascal And C

Famous quotes containing the words type and/or escapes:

    The type of fig leaf which each culture employs to cover its social taboos offers a twofold description of its morality. It reveals that certain unacknowledged behavior exists and it suggests the form that such behavior takes.
    Freda Adler (b. 1934)

    That reality is ‘independent’ means that there is something in every experience that escapes our arbitrary control. If it be a sensible experience it coerces our attention; if a sequence, we cannot invert it; if we compare two terms we can come to only one result. There is a push, an urgency, within our very experience, against which we are on the whole powerless, and which drives us in a direction that is the destiny of our belief.
    William James (1842–1910)