Recent Articles



































Type safety



         


computer science, a programming language is type safe when the language does not permit the programmer to treat a value as a type to which it does not belong. This generally requires that the language have a complete specification of its semantics; this in turn implies that programs written in that language follow the specification regardless of what machine it is run on. Safety is a property of the programming language, however, and not of the programs themselves. For example, it's possible to have a safe program written in a type-unsafe language.

Type safety is synonymous with one of the definitions of strong typing.

Subsets of ML and Java have been mathematically proven to be type-safe (however, certain errors may occur at runtime] due to bugs in the implementation, or in linked libraries written in other languages).

The C programming language is the archtypical type-unsafe language due to casting, particularly of pointers to void pointer and back. More recent implementations of C perform some compile-time type-checking, making the language much more type-safe than it was in its early days. However, the compiler only issues warnings, and the user is still free to perform arbitrary casts that destroy type safety.

In order for a language to be completely type-safe, even against adversaries, it typically needs to have garbage collection. It's possible to make a language type-safe without garbage collection, but it can be difficult; the reason is rather technical: Suppose that a strongly typed language (like Pascal) didn't have garbage collection and that allocated memory had to be explicitly freed. If a dangling pointer existed that still pointed to the old memory location, it's possible that a new data structure can get allocated in the same space with the slot the dangling pointer refers to now pointing to a different type. For example, if the pointer initially pointed to an integer, but in the new space another pointer took its place, then that pointer could be changed to anything simply by changing the value of the integer. Because it is not specified what would happen when such a pointer is changed, the language is not type-safe.

Note that garbage collectors are best implemented in languages that allow pointer arithmetic, so that the library that implements the collector itself is best done in a type-unsafe language like C.

See also: datatype, type theory.

[Top]

Further reading





  View Live Article   This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License