Data Types
Written on 20.16 by Unknown
Before you read this post why don't you watch the trailer of this awesome movie :
Well, after watching that trailer i think it piques your interest to watch the movie when it released right ?
Back to the main topic, here is the answer for the sixth chapter "Data Types" of Sebesta's Programming Language Concepts book
1. What is a descriptor?
- Descriptor is the collection of the attributes of a variable.
- The advantages of decimal data types is being able to precisely store decimal values, at least those within a restricted range, which can’t be done with floating-point. And the disadvantages of decimal data types are that the range of values is restricted because no exponents are allowed, and their representation in memory is mildly wasteful.
The two most important design issues that are specific to character string types are the following:
- Should strings be simply a special kind of character array or a primitive type?
- Should strings have static or dynamic length?
- Static length string: the length can be static and set when the string is created.
- Limited dynamic length strings: allow strings to have varying length up to a declared and fixed maximum set by the variable’s definition.
- Dynamic length strings: allow strings to have varying length with no maximum.
- Ordinal type is one in which the age 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.
- Enumeration and subrange are the two user-defined ordinal type that have been supported by the programming languages.
- 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.
- Subrange type is a contiguous subsequence of an ordinal type.
Problem Set:
1. What are the arguments for and against representing Boolean values as single bits in memory?
- Boolean variables stored as single bits are very space efficient, but on most computers access to them is slower than if they were stored as bytes.
2. How does a decimal value waste memory space?
- Decimal types are stored very much like character strings, using binary codes for the decimal digits. These representations are called binary coded decimal (BCD). In some cases, they are stored one digit per byte, but in others, they are packed two digits per byte. Either way, they take more storage than binary representations. It takes at least four bits to code a decimal digit. Therefore, to store a six-digit coded decimal number requires 24 bits of memory. However, it takes only 20 bits to store the same number in binary.
3. VAX minicomputers use a format for floating-point numbers that is not the same as the IEEE standard. What is this format, and why was it chosen by the designer of the VAX computers? , A reference for VAX floating-point representation is Sebesta (1991).
- The existing DEC VAX formats, inherited from the PDP-11, because The PDP-11 had several uniquely innovative features, and was easier to program than its predecessors through the additional general-purpose registers.
4.Compare the tombstone and lock-and-key methods of avoiding dangling pointers, from the point of view of safety and implementation cost.
- Tombstones are costly in both time and space. Because tombstones are never deallocated, their storage is never reclaimed. Every access to a heap- dynamic variable through a tombstone requires one more level of indirection, which requires an additional machine cycle on most computers. Apparently none of the designers of the more popular languages have found the additional safety to be worth this additional cost, because no widely used language uses tombstones,
- while in lock-&-key pointer values are represented as ordered pairs (key, address), where the key is an integer value. Heap-dynamic variables are represented as the storage for the variable plus a header cell that stores an integer lock value. When a heap-dynamic variable is allocated, a lock value is created and placed both in the lock cell of the heap-dynamic variable and in the key cell of the pointer that is specified in the call to new. Every access to the dereferenced pointer compares the key value of the pointer to the lock value in the heap-dynamic variable. If they match, the access is legal; otherwise the access is treated as a run-time error. Any copies of the pointer value to other pointers must copy the key value. Therefore, any number of pointers can reference a given heap- dynamic variable. When a heap-dynamic variable is deallocated with dispose, its lock value is cleared to an illegal lock value. Then, if a pointer other than the one specified in the dispose is dereferenced, its address value will still be intact, but its key value will no longer match the lock, so the access will not be allowed.
5. What disadvantage are there in implicit dereferencing of pointers, but only in certain contexts? For example, consider the implicit dereference of a pointer to a record in Ada when it is used to reference a record field.
- When implicit dereferencing of pointers occurs only in certain contexts, it makes the language slightly less orthogonal. The context of the reference to the pointer determines its meaning. This detracts from the readability of the language and makes it slightly more difficult to learn.