PLC Session 4

In this fourth session we talked about data type. As usual there are two groups that will give the presentation to the whole class. In this session we broke it down into bunch of topics

  • Introduction
  • Primitive Data Types
  • Character String Types
  • User-Defined Ordinal Types
  • Array Types
  • Associative Arrays
  • Record Types
  • Tuple Types
  • List Types
  • Union Types
  • Pointer and Reference Types
  • Type Checking
  • Strong Typing

Our friend’s group opens the session with introduction. They introduce us what is data type. A data type defines a collection of data values and a set of predefined operations on those values. Computer programs produce results by manipulating data.  A descriptor is the collection of the attributes of variable. The next topic is about Primitive Data Types, data types that are not defined in terms of other types are called primitive data types. Nearly all programming languages provide a set of primitive data types. The primitive data types consist of  numeric types, boolean types, character types. Which numeric types consist of integer, floating-point, complex, and decimal. Next topic is about  Character String Types, A character string type is one in which the values consist of sequences of characters. Character string constants are used to label output, and the input and output of all kinds of data are often done in terms of strings. Strings have their function or operations, the most common string operations are assignment, catenation, substring reference, comparison, and pattern matching. All in all String types have important impact to the writability of a language. Next topic is about User-Defined Ordinal Types.

An ordinal type is one in which the range of possible values can be easily associated with the set of positive integers. In Java, for example, the primitive ordinal types are integer, char, and boolean. There are two user-defined ordinal types that have been supported by programming languages: enumeration and subrange.An enumeration type is one in which all of the possible values, which are named constants, are provided, or enumerated, in the definition. Enumeration types provide a way of defining and grouping collections of named constants, which are called enumeration constants. The definition of a typical enumeration type is shown in the following C# example:

enum days {Mon, Tue, Wed, Thu, Fri, Sat, Sun};

Whereas a subrange type is a contiguous subsequence of an ordinal type. For example, 12..14 is a subrange of integer type. Subrange types were introduced by Pascal and are included in Ada. Enumeration types are usually implemented as integers. Next topic is about Array Types. An array is a homogeneous aggregate of data elements in which an individual element is identified by its position in the aggregate, relative to the first element. The individual data elements of an array are of the same type. For example if an array is integer the whole array would contains integer type. This is called homogeneous array, whereas a heterogeneous array is when an object can be of any types like in Phyton. The most common array operations are assignment, catenation, comparison for equality and inequality, and slices. The C-based languages do not provide any array operations, except through the methods of Java, C++, and C#. Perl supports array assignments but does not support comparisons. The next topic is about associative arrays. An associative array is an unordered collection of data elements that are indexed by an equal number of values called keys. In the case of non-associative arrays, the indices never need to be stored (because of their regularity). In an associative array, however, the user-defined keys must be stored in the structure. So each element of an associative array is in fact a pair of entities, a key and a value.

Moving to other topic that discussed about Record Types. A record is an aggregate of data elements in which the individual elements are identified by names and accessed through offsets from the beginning of the structure. The fields of records are stored in adjacent memory locations. Next topic is about Tuple Types, a tuple is a data type that is similar to a record, except that the elements are not named. Another topic is about List Types. Lists in Scheme and Common LISP are delimited by parentheses and the elements are not separated by any punctuation. For example, (A B C D)

Nested lists have the same form, so we could have (A (B C) D) In this list, (B C) is a list nested inside the outer list. There’s also Union Types, A union is a type whose variables may store different type values at different times during program execution. C and C++ provide union constructs in which there is no language support for type checking. Unions are implemented by simply using the same address for every possible variant. Sufficient storage for the largest variant is allocated. The tag of a discriminated union is stored with the variant in a recordlike structure. At compile time, the complete description of each variant must be stored. This can be done by associating a case table with the tag entry in the descriptor. The case table has an entry for each variant, which points to a descriptor for that particular variant. Not to forget there’s  Pointer and Reference Types.A pointer type is one in which the variables have a range of values that consists of memory addresses and a special value, nil. The value nil is not a valid address and is used to indicate that a pointer cannot currently be used to reference a memory cell. Pointers are designed for two distinct kinds of uses. First, pointers provide some of the power of indirect addressing, which is frequently used in assembly language programming. Second, pointers provide a way to manage dynamic storage. A pointer can be used to access a location in an area where storage is dynamically allocated called a heap, variables that are dynamically allocated from the heap are called heap dynamic variables. Languages that provide a pointer type usually include two fundamental pointer operations: assignment and dereferencing. The first operation sets a pointer variable’s value to some useful address. Dereferencing, is the second fundamental pointer operation, which takes a reference through one level of indirection. The pointer has some problem one of them is a dangling pointer, or dangling reference, is a pointer that contains the address of a heap-dynamic variable that has been deallocated. Dangling pointers are dangerous for several reasons. First, the location being pointed to may have been reallocated to some new heap-dynamic variable. If the new variable is not the same type as the old one, type checks of uses of the dangling pointer are invalid. Even if the new dynamic variable is the same type, its new value will have no relationship to the old pointer’s dereferenced value.

There’s also reference type in this topic, a reference type variable is similar to a pointer, with one important and fundamental difference: A pointer refers to an address in memory, while a reference refers to an object or a value in memory. Move on to the next topic there is type checking.Type checking is the activity of ensuring that the operands of an operator are of compatible types. A compatible type is one that either is legal for the operator or is allowed under language rules to be implicitly converted by compiler-generated code (or the interpreter) to a legal type. This automatic conversion is called a coercion. For example, if an int variable and a float variable are added in Java, the value of the int variable is coerced to float and a floating-point add is done. A type error is the application of an operator to an operand of an inappropriate type. If all bindings of variables to types are static in a language, then type checking can nearly always be done statically. Dynamic type binding requires type checking at run time, which is called dynamic type checking. Last but not least, there is Strong Typing. A programming language is strongly typed if type errors are always detected. This requires that the types of all operands can be determined, either at compile time or at run time. C and C++ are not strongly typed languages because both include union types, which are not type checked.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *