Science  People  Locations  Timeline
Index: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Home > Type safety


In 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 the casting of pointers to void pointers 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 is 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 PascalPascal is one of the landmark computer programming languages on which generations of students cut their teeth and variants of which are still widely used today. TeX and much of the original Macintosh operating system were written in Pascal. The Swiss comp) did not have garbage collection and that allocated memory had to be explicitly released. If a dangling pointerDangling pointers in programming are pointers whose objects have since been deleted or deallocated, without modifying the value of the pointer. In many languages (particularly the C programming language), deleting an object from memory does not alter any existed that still pointed to the old memory location, it is 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 arithmeticpointer. Pointer arithmetic is a particular arithmetic involving pointers, typical of the C programming language. In pointer arithmetic, the unit is the size of the pointer's type. For example, adding 1 to a pointer to integer values (which typically span, so that the library that implements the collector itself is best done in a type-unsafe language like C.

1 See also



Read more »

Non User