0% found this document useful (0 votes)
15 views20 pages

PPL-Unit 2 Part 5

Uploaded by

thirumal536
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views20 pages

PPL-Unit 2 Part 5

Uploaded by

thirumal536
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Principle of programming languages

Unit-II: record , tuple types, list types, union


types, pointer and reference types, type checking,
strong typing, type equivalence
record
•A record is a collection of heterogeneous data
elements
•Records were introduced by COBOL
•In C, C++, and C#, records are called as structures
supported with the struct data type
•Structs are also included in ML and F#.
•In Python and Ruby, records can be implemented as
hashes

Design issues:
• What is the syntactic form of references to fields?
• Are elliptical references allowed?
record
Definitions of Records
The COBOL form of a record declaration
01 EMPLOYEE-RECORD.
02 EMPLOYEE-NAME.
05 FIRST PICTURE IS X(20).
05 MIDDLE PICTURE IS X(10).
05 LAST PICTURE IS X(20).
02 HOURLY-RATE PICTURE IS 99V99.
•The EMPLOYEE-RECORD record consists of the
EMPLOYEE-NAME record andthe HOURLY-RATE field.
•The numerals 01, 02, and 05 that begin the lines of
•the record declaration are level numbers, which
indicate by their relative values the hierarchical
structure of the record.
•Any line that is followed by a line with a higher-level
number is itself a record.
record
The PICTURE clauses show the formats of the field
storage locations, with X(20) specifying 20 alphanumeric
characters and 99V99 specifying four decimal digits with
the decimal point in the middle
In c declaration
struct Person
{
char name[50];
int citNo;
float salary;
};
Ada declaration:
type Employee_Name_Type is record
First : String (1..20);
Middle : String (1..10);
Last : String (1..20);
record
References to Record Fields:

•COBOL field references have the form


field_name OF record_name_1 OF . . . OF
record_name_n
EX:MIDDLE OF EMPLOYEE-NAME OF EMPLOYEE-RECORD
•Ada record example:
Employee_Record.Employee_Name.Middle
Tuple Types
•A tuple is a data type that is similar to a record, except
that the elements are not named.
•Python’s tuples are closely related to its lists.
•A tuple is created by assigning a tuple literal
example:
myTuple = (3, 5.8, 'apple')
•The elements of a tuple need not be of the same type.
•The elements of a tuple can be referenced with indexing
in brackets
myTuple[1]
•They can be deleted with the del statement
•ML includes a tuple data type
•The following statement creates a tuple:
val myTuple = (3, 5.8, 'apple');
•The syntax of a tuple element access is as follows:
#1(myTuple);
List Types
•A List is a data type that is similar to a record, except
that the elements are not named.
•Lists were first supported in the first functional
programming language, LISP.
•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,
(A (B C) D)
•The CAR function returns the first element of its list
parameter.
example:(CAR '(A B C))
•This call to CAR returns A
•The CDR function returns its parameter list minus its
first element
List Types
•Python includes a list data type
For example,consider the following statement:
myList = [3, 5.8, "grape"]
•The elements of a list are referenced with subscripts in
brackets, as in thefollowing example:
x = myList[1]
•This statement assigns 5.8 to x. The elements of a list
are indexed starting at zero.
•A list element can be deleted with del, as in the
following statement:
del myList[1]
•List is also supported ML,F#
Union Types
•A union is a type whose variables may store different
type values at different times during program execution.
Design Issues:
•Should type checking be required? Note that any such
type checking must be dynamic.
• Should unions be embedded in records?
Discriminated Versus Free Unions:
•C and C++ provide union constructs in which there is no
language support for type checking. In C and C++, the
union construct is used to specify union structures. The
unions in these languages are called free unions
For example, consider the following C union:
union flexType {
int intEl;
float floatEl;
};
Union Types
•Type checking of unions requires that each union
construct include a type indicator. Such an indicator is
called a tag, or discriminant , and a union with a
discriminant is called a discriminated union.
•The first language to provide discriminated unions was
ALGOL 68. They are now supported by Ada, ML,Haskell,
and F#.
•Unions in F#
A union is declared in F# with a type statement using OR
operators (|) to define the components. For example, we
could have the following:
type intReal =
| IntValue of int
| RealValue of float;;
Pointer and Reference Types
•The pointer is a variable which stores the address of
another variable.
•In pointer a special value, nil. The value nil is not a valid
address
and is used to indicate that a pointer
•Pointers are designed for two distinct kinds of uses.
•First, pointers provide some of the power of indirect
addressing
•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.
•Pointers are defined using a type operator * in C and C+
+ and access in Ada
Pointer and Reference Types
Design Issues:
The primary design issues particular to pointers are the
following:
• What are the scope and lifetime of a pointer variable?
• What is the lifetime of a heap-dynamic variable (the
value a pointer
references)?
• Are pointers restricted as to the type of value to which
they can point?
• Are pointers used for dynamic storage management,
indirect addressing,
or both?
• Should the language support pointer types, reference
types, or both?
Pointer and Reference Types
Pointer Operations:
• 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 accessing the value of the variable that
the pointer points to..
Pointer and Reference Types
•In C and C++, the asterisk (*) denotes the
dereferencing operation, andthe ampersand (&) denotes
the operator for producing the address of a variable.
•For example, consider the following code:
int *ptr;
int count, init;
...
ptr = &init;
count = *ptr;

•The assignment to the variable ptr sets it to the address


of init.
•The assignment to count dereferences ptr to produce
the value at init, which is then assigned to count.
Pointer and Reference Types
Pointer Problems:
•There are two pointer problems. They are
• Dangling pointer
•Lost heap dynamic variables
Dangling Pointers:
•A dangling pointer, or dangling reference, is a pointer
that contains the address of a heap-dynamic variable
that has been deallocated.
The following sequence of operations creates a dangling
pointer
1. A new heap-dynamic variable is created and pointer
p1 is set to point at it.
2. Pointer p2 is assigned p1’s value.
3. The heap-dynamic variable pointed to by p1 is
explicitly deallocated
(possibly setting p1 to nil), but p2 is not changed by the
Pointer and Reference Types
example, in C++ we could have the following:
int * arrayPtr1;
int * arrayPtr2 = new int[100];
arrayPtr1 = arrayPtr2;
delete [] arrayPtr2;
// Now, arrayPtr1 is dangling, because the heap storage
// to which it was pointing has been deallocated.
Lost Heap-Dynamic Variables:
•A lost heap-dynamic variable is an allocated heap-
dynamic
variable that is no longer accessible to the user program.
Such
variables are often called garbage
•Lost heap-dynamic variables created by the following
sequence of operations:
1. Pointer p1 is set to point to a newly created heap-
Pointer and Reference Types
•The first heap-dynamic variable is now inaccessible, or
lost.This is sometimes called memory leakage.

Reference Types:
A reference type variable is similar to a pointer. A pointer
refers to an address in memory, while a reference refers to
an object or a value in memory.
C++,JAVA, C# are support reference type.
Reference type variables are specified in definitions by
preceding their
names with ampersands (&). For example,
int result = 0;
int &ref_result = result;
...
ref_result = 100;
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.
•For example, in the original version of C, if an int value
was passed to a function that expected a float value, a
type error would occur.
•Dynamic type binding requires type checking at run time,
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.
•Ada is nearly strongly typed
•C and C++ are not strongly typed languages because
both include union types, which are not type checked.
•ML,JAVA,C# are strongly typed
Type Equivalence
•Two types are equivalent if an operand of one type in an
expression is substituted for one of the other type, without
coercion.
•Type equivalence is a strict form of type compatibility—
compatibility without coercion
•There are two approaches to defining type equivalence:
name type equivalence and structure type equivalence.
•Name type equivalence means that two variables have
equivalent types if they are defined either in the same
declaration or in declarations that use the same type
name.
•Structure type equivalence means that two variables have
equivalent types if their types have identical structures.
•Ada declarations:
type Celsius = Float;
Fahrenheit = Float;

You might also like