390 likes | 874 Views
Elementary Data Types. Prof. A lamdeep Singh. Scalar Data Types. Scalar data types represent a single object, i.e. only one value can be derived. In general, scalar objects follow the hardware architecture of a computer. Integers
E N D
Elementary Data Types Prof. Alamdeep Singh
Scalar Data Types • Scalar data types represent a single object, i.e. only one value can be derived.In general, scalar objects follow the hardware architecture of a computer.
Integers SpecificationMaximal and minimal values - depending on the hardware. In some languages these values represented as defined constants. Operations: • Arithmetic • Relational • Assignment • Bit operations • Implementation : Most often using the hardware-defined integer storage representation and a set of hardware arithmetic and relational primitive operations on integers.
Subranges • Specification: A subtype of integer, consists of a sequence of integer values within some restricted range. e.g. a Pascal declaration A: 1..10 means that the variable A may be assigned integer values from 1 through 10. • Implementation: smaller storage requirements, better type checking
Floating-point real numbers • SpecificationOrdered sequence of some hardware-determined minimum negative value to a maximum value. Similar arithmetic, relational and assignment operations as with integers. Roundoff issues - the check for equality may fail due to roundoff. • Implementation: Mantissa - exponent model. The storage is divided into a mantissa - the significant bits of the number, and an exponent. • Example: 10.5 = 0.105 x 102,Mantissa: 105Exponent: 2
Fixed-point real numbers • Specification: Used to represent real numbers with predefined decimal places, such as dollars and cents. • Implementation: May be directly supported by hardware or simulated by software.
Other data typesq • Complex numbers: software simulated with two storage locations - one for the real portion and one for the imaginary portion. • Rational numbers: the quotient of two integers. • Enumerations: ordered list of different values. • Example: enumStudentClass {Fresh, Soph, Junior, Senior}the variable StudentClass may accept only one of the four listed values. • Implementation: represented during run time as integers, correspondeing to the listed values.
Booleans • Specification: Two values: true and false. Can be given explicitly as enumeration,as in Pascal and Ada. Basic operations: and, or, not. • Implementation: A single addressable unit such as byte or word. Two approaches: • Use a particular bit for the value, e.g. the last bit; 1 - true, 0 -false. • Use the entire storage; a zero value would then be false, otherwise - true.
Characters • Specification: Single character as a value of a data object.Collating sequence - the ordering of the characters, used for lexicographic sorting.Operations:RelationalAssignmentTesting the type of the character - e.g. digit, letter, special symbol. • Implementation: usually directly supported by the underlying hardware.
Composite Data Types • Characterized by a complex data structure organization, processed by the compiler. • Character strings: Data objects that are composed of a sequence of characters Specification and syntax. Three basic methods of treatment: • Fixed declared length - storage allocation at translation time • The data object is always a character string of a declared length.Strings longer than the declared length are truncated. • Variable length to a declared bound - storage allocation at translation time. • An upper bound for length is set and any string over that length is truncated • Unbounded length - storage allocation at run time. Strings can be of any length.
Special case: C/C++ • Strings are arrays of charactersNo string type declarationNull character determines the end of a string. Operations • Concatenation – appending two strings one after another • Relational operation on strings – equal, less than, greater than • Substring selection using positioning subscripts • Substring selection using pattern matching • Input/Output formatting • Dynamic strings - the string is evaluated at run time. • Perl: "$ABC" will be evaluated as a name of a variable, and the contents of the variable will be used.
Implementation • Fixed declared length: a packed vector of characters • Variable length to a declared bound: a descriptor that contains the maximum length and the current length • Unbounded length: either a linked storage of fixed-length data objects or a contiguous array of characters with dynamic tun-time storage allocation.
Pointers and programmer-constructed objects • Pointers are variables that contain the location of other data objects • Allow to construct complex data objects. • Used to link together the components of the complex data objects. Specification: • Pointers may reference data objects only of a single type – C, Pascal, Ada. • Pointer may reference data objects of any type. – Smalltalk • C, C++: pointers are data objects and can be manipulated by the programJava: pointers are hidden data structures, managed by the language implementation
Operations: • Creation operation: • Allocates a block of storage for the new data object, and returns its address to be stored in the pointer variable. No name of the location is necessary as the reference would be by the pointer. • Selection operation: the contents of the pointer is used as an address in the memory.
Implementation • Methods: • Absolute addresses stored in the pointer. Allows for storing the new object anywhere in the memory • Relative addresses: offset with respect to some base address. Requires initial allocation of a block of storage to be used by the data objects. The address of each object is relative to the address of the block. Advantages: the entire block can be moved to another location without invalidating the addresses in the pointers, as they are relative, not absolute. • Implementation problems: • Creating objects of different size during execution time requires the management of a general heap storage area. • Garbage - occurs when the contents of pointer is destroyed, and the object still exists however it is no more accessible. • Dangling references: the object is destroyed however the pointer still contains the address of the used location, and can be wrongly used by the program.
Files Characteristics: • Usually reside on secondary storage devices as disks, tapes. • Lifetime is greater than the lifetime of the program that has created the files. • Types of files depending on the method of access • Sequential file: a data structure composed of a linear sequence of components of the same type. • File operations:OpenReadWriteEnd-of-fileClose
Implementation: usually handled by the operating system. • Interactive Input-Output: sequential files used in interactive mode. Direct Access Files: any single component can be accessed at random just as in an array. Key: the subscript to access a component.Implementation: a key table is kept in main memory • Indexed Sequential Files: similar to direct access files using a key combined with capability to process the file sequentially. The file must be ordered by the key
Exam-like questions • What is a scalar data type? Give examples • Describe briefly the implementation of floating-point real numbers. • Describe briefly the implementation of booleans. • What is a composite data type? Give examples. • Describe briefly the approaches to specification and implementation of character strings. • What implementation problems exist with data objects referred to by pointers?