0% found this document useful (0 votes)
29 views174 pages

01 KM 072010004930012

The document discusses data types in programming, including their definitions, uses, and evolution. It covers primitive data types, character string types, user-defined ordinal types, and arrays, highlighting their characteristics and implementation in various programming languages. Additionally, it addresses type binding, type checking, and the importance of data types in program organization and error prevention.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views174 pages

01 KM 072010004930012

The document discusses data types in programming, including their definitions, uses, and evolution. It covers primitive data types, character string types, user-defined ordinal types, and arrays, highlighting their characteristics and implementation in various programming languages. Additionally, it addresses type binding, type checking, and the importance of data types in program organization and error prevention.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 174

Introduction

• What do you mean when you declare


int n;
– Possible values of n?
– Possible operations on n?
• How about freshman defined below?
typedef struct {
char name[30];
int student_number;
} Student;
Student freshman = {“John", 644101};

Dr. K. V. Metre, Dept. of Computer


0
Engg. ioe, nashik
Data Type
• A data type is a set
– When you declare that a variable has a certain type,
you are saying that
(1) the values that the variable can have are elements
of a certain set, and
(2) there is a collection of operations that can be
applied to those values
• Fundamental issues for PL designers:
– How to define a sufficient set of data types?
– What operations are defined and how are they
specified for a data type?
Dr. K. V. Metre, Dept. of Computer
1
Engg. ioe, nashik
Evolution of Data Types
• Earlier PL’s tried to include many data types to
support a wide variety of applications, e.g. PL/I
• Wisdom from ALGOL 68:
– A few basic types with a few defining operators for
users to define their own according to their needs
• From user-defined type to abstract data type
– The interface of a type (visible to user) is separated
from the representation and set of operations on
values of that type (invisible to user)

Dr. K. V. Metre, Dept. of Computer


2
Engg. ioe, nashik
Uses for Types
• Program organization and documentation
– Separate types for separate concepts
• Represent concepts from problem domain
– Indicate intended use of declared identifiers
• Types can be checked, unlike program comments
• Identify and prevent errors
– Compile-time or run-time checking can prevent
meaningless computations, e.g., 3+TRUE–“Bill”
• Support optimization
– Example: short integers require fewer bits
– Access record component by known offset

Dr. K. V. Metre, Dept. of Computer


3
Engg. ioe, nashik
Overview
• Various Data Types (Sec. 6.2~6.9)
– Primitive Data Types, Character String Types, User-
Defined Ordinal Types, Array Types, Associative Arrays,
Record Types, Union Types, Pointer and Reference
Types
• Type Binding (Sec. 5.4.2)
• Type Checking (Sec. 6.10)
• Strong Typing (Sec. 6.11)
• Type Equivalence (Sec. 6.12)
• Theory and Data Types (Sec. 6.13)
Dr. K. V. Metre, Dept. of Computer
4
Engg. ioe, nashik
Primitive Data Types
• Almost all programming languages provide a
set of primitive data types
– Those not defined in terms of other data types
– Integer, floating-point, Boolean, character
• Some primitive data types are merely
reflections of the hardware
• Others require only a little non-hardware
support for their implementation

Dr. K. V. Metre, Dept. of Computer


5
Engg. ioe, nashik
Character String Types
• Values are sequences of characters
• Design issues:
– Is it a primitive type or just a special kind of array?
– Should the length of strings be static or dynamic?
– What kinds of string operations are allowed?
• Assignment and copying: what if have diff. lengths?
• Comparison (=, >, etc.)
• Catenation
• Substring reference: reference to a substring
• Pattern matching
Dr. K. V. Metre, Dept. of Computer
6
Engg. ioe, nashik
Character String in C
• C and C++
– Not primitive; use char arrays, terminate with a null
character (0)
– A library of functions to provide operations instead of
primitive operators
– Problems with C string library:
• Functions in library do not guard against overflowing the
destination, e.g., strcpy(src, dest);

• Java
– Primitive via the String and StringBuffer class
Dr. K. V. Metre, Dept. of Computer
7
Engg. ioe, nashik
• char firstStr[] = "This is Cstyle string";
• char secStr[100] = {'s','o','f','t','w','a','r','e','
','t','e','s','t','i','n','g',' ','h','e','l','p','\0'};
• char thirdStr[] = {'h','e','l','l','o','\0'};

Dr. K. V. Metre, Dept. of Computer


8
Engg. ioe, nashik
#include <iostream>
#include <string>
using namespace std;
int main()
{
char firstStr[100];
cin>>firstStr;
cout<<"you entered : "<<firstStr;
}

Dr. K. V. Metre, Dept. of Computer


9
Engg. ioe, nashik
• Strings in C: Strings are defined as an array of
characters. The difference between a character array
and a string is the string is terminated with a special
character ‘\0’.
strcat: The strcat() function will append a copy of the
source string to the end of destination string. The
strcat() function takes two arguments:
1) dest
2) src
It will append copy of the source string in the
destination string. The terminating character at the
end of dest is replaced by the first character of src .
Return value: The strcat() function returns dest, the
pointer to the destination string.

Dr. K. V. Metre, Dept. of Computer


10
Engg. ioe, nashik
• / CPP program to demonstrate // strcat
• #include <cstring>
• #include <iostream>
• using namespace std;
• int main()
• {
• char dest[50] = "This is an";
• char src[50] = " example";

• strcat(dest, src);
• cout << dest;
• return 0;
• }
Dr. K. V. Metre, Dept. of Computer
11
Engg. ioe, nashik
• strcmp: strcmp() is a built-in library function
and is declared in <string.h> header file.
• This function takes two strings as arguments
and compare these two strings
lexicographically.
Syntax::
• int strcmp(const char *leftStr, const char
*rightStr );

Dr. K. V. Metre, Dept. of Computer


12
Engg. ioe, nashik
• strcpy: strcpy() is a standard library function in C/C++ and is used
to copy one string to another. In C it is present in string.h header
file and in C++ it is present in cstring header file.
char* strcpy(char* dest, const char* src);
• Parameters: This method accepts following parameters:
– dest: Pointer to the destination array where the
content is to be copied.
src: string which will be copied.
• strlen: The strlen() function calculates the length of a given
string.
• The strlen() function is defined in string.h header file.
• It doesn’t count null character ‘\0’.
Syntax:
• int strlen(const char *str);
Dr. K. V. Metre, Dept. of Computer
13
Engg. ioe, nashik
• No FunctionArguments Description
• 1 strcpy s1, s2 Copies string s2 into string s1
• 2 strcats1,s2 Appends or concatenates string s2 to
the end of string s1
• 3strlens1Returns length of string s14strcmps1,s2Returns
0: s1 and s2 are same
<0:s1 <s2
>0:s1>s2</s2
5strchrs1,chReturns a pointer to first occurrence of
character ch in string s1and the string from there
onwards6strstrs1,s2Returns first occurrence of string s2
in string s1
Dr. K. V. Metre, Dept. of Computer
14
Engg. ioe, nashik
• #include <iostream>
• #include <cstring>
• using namespace std;
• int main()
• {
• char firstStr[50] = "This is softwareTestingHelp.com";
• char secStr[50] = "a testing platform";
• &nbsp;
• int len = strlen(firstStr);
• cout<<"Length of firstStr : "<<len;
• strcpy(secStr,"softwareTesting");
• cout<<"\nResultant string (secStr):"<<secStr;
• cout<<"\nComparing firstStr and secStr :"<<strcmp(firstStr,secStr);
• strcat(secStr," for you");
• cout<<"\nConcatenated secStr: "<<secStr;
• cout<<"\nFirst occurence of i in firststr at:"<<strchr(firstStr,'T');
• return 0;
• }

Dr. K. V. Metre, Dept. of Computer


15
Engg. ioe, nashik
Character String Length Options

• Static: COBOL, Java’s String class


– Length is set when string is created
• Limited dynamic length: C and C++
– Length varying, up to a fixed maximum
– In C-based language, a special character is used to
indicate the end of a string’s characters, rather than
maintaining the length
• Dynamic (no maximum): SNOBOL4, Perl,
JavaScript
• Ada supports all three string length options
Dr. K. V. Metre, Dept. of Computer
16
Engg. ioe, nashik
Character String Implementation

• Static length: compile-time descriptor


• Limited dynamic length: may need a run-time
descriptor for length
• Dynamic length: need run-time descriptor;
allocation/deallocation is the biggest implementation
problem

Dr. K. V. Metre, Dept. of Computer


17
Engg. ioe, nashik
User-Defined Ordinal Types

• Ordinal type:
range of possible values can be easily associated with
positive integers
• Two common user-defined ordinal types
– Enumeration: All possible values, which are named
constants, are provided or enumerated in the
definition, e.g., C# example
enum days {mon, tue, wed, thu, fri,
sat, sun};
– Subrange: An ordered contiguous subsequence of an
ordinal type, e.g., 12..18 is a subrange of integer
type

Dr. K. V. Metre, Dept. of Computer


18
Engg. ioe, nashik
Enumeration Types
• A common representation is to treat the values of an
enumeration as small integers
– May even be exposed to the programmer, as is in
C:
enum coin {penny=1, nickel=5, dime=10, quarter=25};
enum escapes {BELL='\a', BACKSPACE='\b', TAB='\t',
NEWLINE='\n', VTAB='\v', RETURN='\r' };

• If integer nature of representation is exposed,


may allow some or all integer operations:
C: int x = penny + nickel + dime;
– enum boolean { false, true }; // inside function
– enum boolean check;
Dr. K. V. Metre, Dept. of Computer
19
Engg. ioe, nashik
#include <iostream>
using namespace std;
enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday };

int main() {
week today;
today = Wednesday;
cout << "Day " << today+1;
return 0;
}
Output
Day 4

Dr. K. V. Metre, Dept. of Computer


20
Engg. ioe, nashik
• Example2: Changing Default Value of Enums
#include <iostream>
using namespace std;
enum seasons { spring = 34, summer = 4, autumn = 9,
winter = 32};
int main() {
seasons s;
s = summer;
cout << "Summer = " << s << endl;
return 0;
}
Output
Summer = 4

Dr. K. V. Metre, Dept. of Computer


21
Engg. ioe, nashik
Evaluation of Enumerated Type

• Aid to readability, e.g., no need to code a color as a


number
• Aid to reliability, e.g., compiler can check:
– Operations (don’t allow colors to be added)
– No enumeration variable can be assigned a value
outside its defined range
– Ada, C#, and Java 5.0 provide better support for
enumeration than C++ because enumeration type
variables in these languages are not coerced into
integer types. Color c; c=4;
Dr. K. V. Metre, Dept. of Computer
22
Engg. ioe, nashik
Subrange Types

• Ada’s design
– Not new type, but rename of constrained versions
type Days is (mon, tue, wed, thu, fri,
sat, sun);
subtype Weekdays is Days range
mon..fri;
subtype Index is Integer range 1..100;
Day1: Days;
Day2: Weekday;
Day2 := Day1;

Dr. K. V. Metre, Dept. of Computer


23
Engg. ioe, nashik
Subrange Types
• Usually, we just use the same representation for the
subtype as for the supertype
– May be with code inserted (by the compiler) to
restrict assignments to subrange variables
• Subrange evaluation
– Aid to readability: make it clear to the readers that
variables of subrange can store only certain range
of values
– Reliability: assigning a value to a subrange variable
that is outside specified range is detected as an
error

Dr. K. V. Metre, Dept. of Computer


24
Engg. ioe, nashik
Array Types
• Array: an aggregate of homogeneous data elements, in which an
individual element is identified by its position in the aggregate
– Array indexing (or subscripting): a mapping from indices to
elements
– Two types in arrays: element type, index type
• Array index types:
– FORTRAN, C: integer only
– Pascal: any ordinal type (integer, Boolean, char)
– Java: integer types only
– C, C++, Perl, and Fortran do not specify range checking, while
Java, ML, C# do
Dr. K. V. Metre, Dept. of Computer
25
Engg. ioe, nashik
Categories of Arrays
• Static:
subscript ranges are statically bound and storage
allocation is static (before run-time)
– Advantage: efficiency (no dynamic allocation)
– C and C++ arrays that include static modifier
• Fixed stack-dynamic:
subscript ranges are statically bound, but the
allocation is done at declaration time during
execution. Blocks
– Advantage: space efficiency
– C and C++ arrays without static modifier
Dr. K. V. Metre, Dept. of Computer
26
Engg. ioe, nashik
Categories of Arrays (cont.)
• Stack-dynamic:
subscript ranges and storage allocation are dynamically
bound at elaboration time and remain fixed during
variable lifetime
– Advantage: flexibility (the size of an array need not
be known until the array is to be used), e.g., Ada
Get(List_Len); // input array size
declare
List: array (1..List_Len) of Integer;
begin
...
End // List array deallocated
Dr. K. V. Metre, Dept. of Computer
27
Engg. ioe, nashik
Categories of Arrays (cont.)
• Fixed heap-dynamic:
storage binding is dynamic but fixed after allocation,
allocated from heap
– C and C++ through malloc
• Heap-dynamic:
binding of subscript ranges and storage is dynamic
and can change
– Advantage: flexibility (arrays can grow or shrink during
execution), e.g., Perl, JavaScript
– C#: through ArrayList; objects created without
element and added later with Add
ArrayList intList = new ArrayList();
intList.Add(nextOne);

Dr. K. V. Metre, Dept. of Computer


28
Engg. ioe, nashik
Array Initialization and Operations
• Some languages allow initialization at the time of storage
allocation
– C, C++, Java, C# example
int list [] = {4, 5, 7, 83}
– Java initialization of String objects
String[] names = {“Bob”, “Jake”, “Joe”};
• Ada allows array assignment and catenation
• Fortran provides elemental operations, e.g.,
– + operator between two arrays results in an array of
the sums of the element pairs of the two arrays

Dr. K. V. Metre, Dept. of Computer


29
Engg. ioe, nashik
• Rectangular and Jagged Arrays
• A rectangular array is a multidimensioned array in which
all of the rows have the same number of elements and
all of the columns have the same number of elements.
Rectangular arrays model rectangular tables exactly.
• A jagged array is one in which the lengths of the rows
need not be the same

Dr. K. V. Metre, Dept. of Computer


30
Engg. ioe, nashik
• arr[0] = new int[4];
• arr[1] = new int[6];
• arr[0] = new int[4] { 11, 21, 56, 78 };
• arr[1] = new int[6] { 42, 61, 37, 41, 59, 63 };
• Int a[3][] ={ {1,2,3},
{12, 45,23, 5};
{5,8}};

Dr. K. V. Metre, Dept. of Computer


31
Engg. ioe, nashik
Slices
• A slice of an array is some substructure of that
array.
• For example, if A is a matrix, then the first row
of A is one possible slice, as are the last row
and the first column.
• It is important to realize that a slice is not a
new data type.
• A=

Dr. K. V. Metre, Dept. of Computer


32
Engg. ioe, nashik
• In Perl,
• mat = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
• mat[1] refers to the second row of mat
• mat[0][0:2] refers to the first and second
element of the first row of mat, which is [1, 2].

Dr. K. V. Metre, Dept. of Computer


33
Engg. ioe, nashik
Compile- time descriptor for single- dimensioned arrays

Dr. K. V. Metre, Dept. of Computer


34
Engg. ioe, nashik
• In row major order, If the array is a matrix, it is stored by
rows. For example, if the matrix had the values
347
625
138
• it would be stored in row major order as
• 3, 4, 7, 6, 2, 5, 1, 3, 8

Dr. K. V. Metre, Dept. of Computer


35
Engg. ioe, nashik
Associative Arrays
• An unordered collection of data elements indexed by an equal number
of values (keys)
– User defined keys must be stored
• Perl:
• %lookup = ("dave", 1234,
"peter", 3456,
"andrew", 6789);
• The reference a particular value you do:
$lookup{"dave"}
• You can create new elements by assignments to new keys. E.g.
$lookup{"adam"} = 3845;
• We can do new assignments to old keys also:
• # change dave's code $lookup{"dave"} = 7634;
• Elements can be removed with delete
• delete $lookup{"dave"} ;
Dr. K. V. Metre, Dept. of Computer
36
Engg. ioe, nashik
Record Types
• A record is a possibly heterogeneous aggregate of data
elements in which the individual elements are identified
by names
• COBOL uses level numbers to show nested records
(others use recursive definition)
01 EMP-REC.
02 EMP-NAME.
05 FIRST PIC X(20).
05 MID PIC X(10).
05 LAST PIC X(20).
02 HOURLY-RATE PIC 99V99.

Dr. K. V. Metre, Dept. of Computer


37
Engg. ioe, nashik
References and Operations

1. COBOL
– field_name OF record_name_1 OF ... OF record_name_n
2. Others (dot notation)
– record_name_1.record_name_2. ...
record_name_n.field_name
• Operations:
– Assignment is common if types are identical
– Ada allows record comparison
– COBOL provides MOVE CORRESPONDING
• Copies a field of the source record to the corresponding
field in the target record

Dr. K. V. Metre, Dept. of Computer


38
Engg. ioe, nashik
• In C, C++, and C#, records are supported with the struct
data type.
• In C++, structures are a minor variation on classes.
• In C#, structs also are related to classes, but are quite
different from them.
• C# structs are stack- allocated value types, as opposed
to class objects, which are heap- allocated reference
types.
• In Python and Ruby, records can be implemented as
hashes, which themselves can be elements of arrays.

Dr. K. V. Metre, Dept. of Computer


39
Engg. ioe, nashik
Unions Types
• A union is a type whose variables are allowed to
store different types of values at different times
union time {
long simpleDate;
double perciseDate;} mytime;
...
printTime(mytime.perciseDate);
• Design issues
– Should type checking be required?
– Should unions be embedded in records?

Dr. K. V. Metre, Dept. of Computer


40
Engg. ioe, nashik
Discriminated vs. Free Unions

• In Fortran, C, and C++, no language-supported type


checking for union, called free union
• Most common way of remembering what is in a
union is to embed it in a structure
struct var_type {
union {
float un_float;
int un_int; } vt_un;
} var_type;

Dr. K. V. Metre, Dept. of Computer


41
Engg. ioe, nashik
#include <stdio.h>
#include <string.h>
union Data {
int i;
char str[20];
};
int main( )
{ union Data std;
std.i = 10;
printf( “std is : %d\n", std.i);
}
Dr. K. V. Metre, Dept. of Computer
42
Engg. ioe, nashik
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
// data.f = 220.5;
// strcpy( data.str, "C Programming");
printf( "data.i : %d\n", data.i);
//printf( "data.f : %f\n", data.f);
//printf( "data.str : %s\n", data.str);
return 0;
}

Dr. K. V. Metre, Dept. of Computer


43
Engg. ioe, nashik
Discriminated vs. Free Unions

• Discriminated union:
in order to type-checking unions, each union
includes a type indicator called a discriminant
– Supported by Ada
– Can be changed only by assigning entire record,
including discriminant  no inconsistent records

Dr. K. V. Metre, Dept. of Computer


44
Engg. ioe, nashik
Ada Union Types
type Shape is (Circle, Triangle, Rectangle);
type Colors is (Red, Green, Blue);
type Figure (Form: Shape) is record
Filled: Boolean;
Color: Colors;
case Form is
when Circle => Diameter: Float;
when Triangle =>
Leftside, Rightside: Integer;
Angle: Float;
when Rectangle => Side1, Side2: Integer;
end case;
end record;
Figure_1 := (Filled => True,
Color => Blue, Form => Rectangle,
Side_1 => 2, Sice_2 => 3);
Dr. K. V. Metre, Dept. of Computer
45
Engg. ioe, nashik
Ada Union Types

• A discriminated union of three shape variables

Dr. K. V. Metre, Dept. of Computer


46
Engg. ioe, nashik
Evaluation of Unions

• Free unions are unsafe


– Do not allow type checking
• Java and C# do not support unions
– Reflective of growing concerns for safety in
programming language
• Ada’s descriminated unions are safe

Dr. K. V. Metre, Dept. of Computer


47
Engg. ioe, nashik
Pointer and Reference Types

• A pointer type variable has 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.
• Provide the power of indirect addressing
• Provide a way to manage dynamic memory
• A pointer can be used to access a location in the area
where storage is dynamically created (heap)

Dr. K. V. Metre, Dept. of Computer


48
Engg. ioe, nashik
Design Issues of Pointers

• What are the scope of and lifetime of a pointer variable?


• What is the lifetime of a heap-dynamic variable?
• 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?
Dr. K. V. Metre, Dept. of Computer
49
Engg. ioe, nashik
Pointer Operations

• Two fundamental operations:


assignment and dereferencing
• Assignment sets a pointer variable’s value to some
useful address
• Dereferencing yields the value stored at the location
represented by the pointer’s value
– Dereferencing can be explicit or implicit
– C and C++ uses an explicit operator *
j = *ptr
sets j to the value located at ptr

Dr. K. V. Metre, Dept. of Computer


50
Engg. ioe, nashik
Pointer Assignment

• The assignment operation j = *ptr

Dr. K. V. Metre, Dept. of Computer


51
Engg. ioe, nashik
Problems with Pointers
• Dangling pointers (dangerous)
– A pointer points to a heap-dynamic variable that has
been deallocated
 has pointer but no storage
– What happen when deferencing a dangling pointer?
• Lost heap-dynamic variable
– An allocated heap-dynamic variable that is no longer
accessible to the user program (often called garbage)
 has storage but no pointer
• The process of losing heap-dynamic variables is
called memory leakage
• 1. Pointer p1 is set to point to a newly created heap- dynamic variable.
• 2. p1 is later set to point to another newly created heap-dynamic variable.
Dr. K. V. Metre, Dept. of Computer
52
Engg. ioe, nashik
Dr. K. V. Metre, Dept. of Computer
53
Engg. ioe, nashik
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.

Dr. K. V. Metre, Dept. of Computer


54
Engg. ioe, nashik
Pointers in C and C++
• Extremely flexible but must be used with care
• Pointers can point at any variable regardless of when
it was allocated
• Used for dynamic storage management and
addressing
• Pointer arithmetic is possible
• Explicit dereferencing and address-of operators
• Domain type need not be fixed (void *)
– void * can point to any type and can be type
checked (cannot be de-referenced)

Dr. K. V. Metre, Dept. of Computer


55
Engg. ioe, nashik
Pointer Arithmetic in C and C++

float stuff[100];
float *p;
p = stuff;

*(p+5) is equivalent to stuff[5] and p[5]


*(p+i) is equivalent to stuff[i] and p[i]
The expression p -> age is equivalent to (*p).age.

Dr. K. V. Metre, Dept. of Computer


56
Engg. ioe, nashik
Reference Types

• A reference type variable refers to an object or a value in memory,


while a pointer refers to an address
 not sensible to do arithmetic on references
• C++ reference type variable: a constant pointer that is implicitly
dereferenced; primarily for formal parameters  initialized with
address at definition, remain constant
int result;
int &ref_result = result;
ref_result = 100;
• Java uses reference variables to replace pointers entirely
– Not constants, can be assigned; reference to class instances
Student s1 = new Student();

Dr. K. V. Metre, Dept. of Computer


57
Engg. ioe, nashik
Evaluation of Pointers

• Dangling pointers and dangling objects are problems


as is heap management

• Pointers are like goto's--they widen the range of


cells that can be accessed by a variable

• Pointers or references are necessary for dynamic


data structures--so we can't design a language
without them

Dr. K. V. Metre, Dept. of Computer


58
Engg. ioe, nashik
• A dangling pointer, or dangling reference, is a
pointer that contains the address of a heap-
dynamic variable that has been deallocated.

Dr. K. V. Metre, Dept. of Computer


59
Engg. ioe, nashik
Dangling Pointer Problem
• Tombstone: extra heap cell that is a pointer to
the heap-dynamic variable
– The actual pointer variable points only at tombstones
– When heap-dynamic variable de-allocated,
tombstone remains but is set to nil
– Any pointer variables pointing to that heap-dynamic
variable will know it is gone by noticing tombstone
becomes nil
– Costly in time and space

Dr. K. V. Metre, Dept. of Computer


60
Engg. ioe, nashik
Dangling Pointer Problem
• Locks-and-keys:
– Heap-dynamic variable represented as (variable, lock)
– Associated pointer represented as (key, address)
– When heap-dynamic variable allocated, a lock is
placed in lock cell of that variable as well as the key
cell of the corresponding pointer variable
– Any copies of the pointer value to other pointer
variables must also copy the key value
– When a heap-dynamic variable is deallocated, its lock
value is cleared to an nil
– Any remaining pointers will have a mismatch

Dr. K. V. Metre, Dept. of Computer


61
Engg. ioe, nashik
Expressions and
Assignment Statements
Introduction
• Expressions are the fundamental means of
specifying computations in a PL
– While variables are the means for specifying storage
• Primary use of expressions: assignment
– Main purpose: change the value of a variable
– Essence of all imperative PLs: expressions change
contents of variables (computations change states)
• To understand expression evaluation, need to
know orders of operator and operand evaluation
– Dictated by associativity and precedence rules
Dr. K. V. Metre, Dept. of Computer
63
Engg. ioe, nashik
Outline
• Arithmetic Expressions
• Overloaded Operators
• Type Conversions
• Relational and Boolean Expressions
• Short-Circuit Evaluation
• Assignment Statements
• Mixed-Mode Assignment

Dr. K. V. Metre, Dept. of Computer


64
Engg. ioe, nashik
Arithmetic Expressions
• Arithmetic expressions consist of operators, operands,
parentheses, and function calls
– Unary, binary, ternary (e.g., _?_:_) operators
• Implementation involves:
– Fetching operands, usually from memory
– Executing arithmetic operations on the operands
• Design issues for arithmetic expressions
– Operator precedence/associativity rules?
– Order of operand evaluation and their side effects?
– Operator overloading?
– Type mixing in expressions?
Dr. K. V. Metre, Dept. of Computer
65
Engg. ioe, nashik
Operator Precedence Rules
• Define the order in which “adjacent” operators of
different precedence levels are evaluated
– Based on the hierarchy of operator priorities
• Typical precedence levels
– parentheses
– unary operators
– ** (exponentiation, if the language supports it)
– *, /
– +, -
Dr. K. V. Metre, Dept. of Computer
66
Engg. ioe, nashik
Operator Associativity Rule
• Define the order in which adjacent operators
with the same precedence level are evaluated
• Typical associativity rules
– Left to right, except **, which is right to left
– Sometimes unary operators associate right to left
• Precedence and associativity rules can be
overridden with parentheses

Dr. K. V. Metre, Dept. of Computer


67
Engg. ioe, nashik
Conditional Expressions
• Conditional expressions by ternary operator ?:
– C-based languages (e.g., C, C++), e.g.,
average = (count == 0)? 0 : sum / count

– Evaluates as if written like


if (count == 0)
average = 0
else
average = sum /count
Dr. K. V. Metre, Dept. of Computer
68
Engg. ioe, nashik
Operand Evaluation Order

• How operands in expressions are “evaluated”?


– Variables: fetch the value from memory
– Constants: sometimes fetched from memory;
sometimes in the machine language instruction
– Parenthesized expressions: evaluate all inside
operands and operators first
– Operands on the two sides of an operator:
evaluation order is usually irrelevant, except when
the operand may cause side effects, e.g.,
b = a + foo(&a);
Dr. K. V. Metre, Dept. of Computer
69
Engg. ioe, nashik
Side Effects in Expressions
• Functional side effects:
• a function changes a two-way parameter or a non-local
variable
– i.e., change the state “external” to the function
• Problem with functional side effects:
– When a function referenced in an expression alters
another operand of the expression:
a = 10;
/* assume foo changes its parameter */
b = a + foo(&a);

Dr. K. V. Metre, Dept. of Computer


70
Engg. ioe, nashik
Functional Side Effects
• Functions in pure mathematics do not have side effects,
i.e., y = f(x)
– Input, x, determines output, y; no states

• Same with pure functional programming languages

• Side effects occur due to von Neumann arch. and


associated imperative PL and computation model
(state machines)
– Memory/processor, variables/expressions, state/state
change
Dr. K. V. Metre, Dept. of Computer
71
Engg. ioe, nashik
Functional Side Effects
• Solution 1: define the language by disallowing functional
side effects
– No two-way parameters in functions
– No non-local references in functions
– Disadvantage: inflexibility of one-way parameters and lack of
non-local references

• Solution 2: write the language definition to demand that


operand evaluation order be fixed
– Disadvantage: limits some compiler optimizations
– Java requires that operands appear to be evaluated in left-to-
right order

Dr. K. V. Metre, Dept. of Computer


72
Engg. ioe, nashik
Overloaded Operators
int a,b;
float x,y;

b = a + 3;
y = x + 3.0;
• We wish to use the same operator ‘+’ to operate on
integers and floating-point numbers
– Let compiler make proper translation, e.g., ADD vs FADD
– ‘+’ to operate on two array variables

Dr. K. V. Metre, Dept. of Computer


73
Engg. ioe, nashik
Overloaded Operators
• Use of an operator for more than one purpose is called
operator overloading
• Some are common (e.g., + for int and float)
• Some are troublesome (e.g., * in C and C++)
– Loss of compiler error detection (omission of an operand
should be a detectable error)
– Some loss of readability
• C++/C# allow user-defined overloaded operator
– Users can define nonsense operations
– Readability may suffer, even when operators make sense,
e.g., need to check operand types to know

Dr. K. V. Metre, Dept. of Computer


74
Engg. ioe, nashik
Type Conversions
int a,b;
float x,y;
a = y;
x = b;
b = y + a;
• How should data be converted for assignment?
• What kinds of data format should compiler
use during evaluation of the expressions?

Dr. K. V. Metre, Dept. of Computer


75
Engg. ioe, nashik
Type Conversions
• A narrowing conversion is one that converts an object
to a type that cannot include all of the values of the
original type, e.g., float to int
– Not always safe

• A widening conversion is one in which an object is


converted to a type that can include at least
approximations to all of the values of the original type,
e.g., int to float
– Usually safe

Dr. K. V. Metre, Dept. of Computer


76
Engg. ioe, nashik
Type Conversions: Mixed Mode
• A mixed-mode expression is one that has
operands of different types
– Need type conversion implicitly or explicitly

• Implicit type conversion by compiler: coercion


– In most languages, all numeric types are coerced
in expressions, using widening conversions
– In Ada, there are virtually no coercions in
expressions to minimize errors due to mixed-
mode expressions

Dr. K. V. Metre, Dept. of Computer


77
Engg. ioe, nashik
Type Conversions
• Explicit type conversion by programmer:
• casting in C-based languages, e.g.,
– C: (int) angle
– Ada: Float (Sum)

• Causes of errors in expressions


– Inherent limitations of arithmetic, e.g., division by
zero
– Limitations of computer arithmetic, e.g. overflow

• Errors often ignored by the run-time system


Dr. K. V. Metre, Dept. of Computer
78
Engg. ioe, nashik
Relational Expressions
• Expressions using relational operators and
operands of various types; evaluate to
Boolean
– Relational operators: compare values of 2
operands
– Operator symbols vary among languages (!=, <=,
>=, /=, ~=, .NE., <>, #)

Dr. K. V. Metre, Dept. of Computer


79
Engg. ioe, nashik
Boolean Expressions
• Expressions using Boolean operators and
Boolean operands, and evaluate to Boolean
– Boolean operands: Boolean variables, Boolean
constants, relational expressions
– Example operators:
FORTRAN 77 FORTRAN 90 C Ada
.AND. and && and
.OR. or || or
.NOT. not ! not
Dr. K. V. Metre, Dept. of Computer
80
Engg. ioe, nashik
No Boolean Type in C

• C89 has no Boolean type:


it uses int type with 0 for false and nonzero for true
– Expression evaluates to 0 for false and 1 for true

• One odd characteristic of C’s expressions:


a < b < c is a legal expression, but the result is not
what you might expect:
– Left operator is evaluated, producing 0 or 1
– The evaluation result is then compared with the third
operand (i.e., c)
Dr. K. V. Metre, Dept. of Computer
81
Engg. ioe, nashik
Precedence Operators in C

Highest postfix ++, --


unary +, -, prefix ++, --, !
*, /, %
binary +, -
<, >, <=, >=
=, !=
&&
Lowest ||
Dr. K. V. Metre, Dept. of Computer
82
Engg. ioe, nashik
Short Circuit Evaluation
• A short-circuit evaluation of an expression is one in
which the result is determined w/o evaluating all
operands and/or operators
(13*a) * (b/13–1)
– If a is zero, there is no need to evaluate (b/13-1)
(a >= 0) && (b < 10)

• Problem with non-short-circuit evaluation


index = 0;
while (index < listlen) && (LIST[index]
!= key)
index = index + 1;
– When index == listlen,
– LIST[index] causes an indexing problem (if LIST has
listlen-1 elements)
Dr. K. V. Metre, Dept. of Computer
83
Engg. ioe, nashik
Short Circuit Evaluation
• C, C++, and Java:
use short-circuit evaluation for the usual Boolean operators (&&
and ||), but also provide bitwise Boolean operators that are not
short circuit (& and |)
• Ada:
programmer can specify either (short-circuit is specified with
and then and or else)
• Short-circuit evaluation exposes the potential problem of side
effects in expressions
e.g., (a > b) || (b++ / 3)
• b is changed (in the second arithmetic expression) only when a
Dr. K. V. Metre, Dept. of Computer
<= b. Engg. ioe, nashik
84
Assignment Statements
• The general syntax
<target_var> <assign_operator> <expression>
• The assignment operator
– = FORTRAN, BASIC, the C-based languages
– := ALGOLs, Pascal, Ada
• = can be bad when it is overloaded for the
relational operator for equality (that’s why the C-
based languages use == as the relational operator)
Dr. K. V. Metre, Dept. of Computer
85
Engg. ioe, nashik
Conditional Targets
• Conditional targets (Perl)
($flag ? $total : $subtotal) = 0

– Which is equivalent to

if ($flag){
$total = 0
} else {
$subtotal = 0
}
Dr. K. V. Metre, Dept. of Computer
86
Engg. ioe, nashik
Compound Assignment Operators
• A shorthand method of specifying a
commonly needed form of assignment
• Introduced in ALGOL; adopted by C
• Example:
a = a + b

is written as

a += b
Dr. K. V. Metre, Dept. of Computer
87
Engg. ioe, nashik
Unary Assignment Operators
• Unary assignment operators in C-based languages
combine increment and decrement operations
with assignment
• Examples:
– sum = ++count (count incremented, assigned to
sum)
– sum = count++ (count assigned to sum and then
incremented)
– count++ (count incremented)
– -count++ (count incremented then negated)

Dr. K. V. Metre, Dept. of Computer


88
Engg. ioe, nashik
Assignment as an Expression
• In C, C++, and Java, the assignment statement
produces a result and can be used as operands
while ((ch = getchar())!= EOF){…}
– ch = getchar() is carried out; result is used as a
conditional value for the while statement
– Has expression side effect: a=b+(c=d/b)-1
– Multiple-target assignment: sum = count = 0;
– Hard to tell: if (x = y) and if (x == y)
• Perl and Ruby support list assignments, e.g.,
($first, $second, $third) = (20, 30, 40);

Dr. K. V. Metre, Dept. of Computer


89
Engg. ioe, nashik
Mixed-Mode Assignment
• Assignment statements can also be mixed-
mode
• In Fortran, C, and C++, any numeric type value
can be assigned to any numeric type variable
• In Java, only widening assignment coercions
are done
• In Ada, there is no assignment coercion

Dr. K. V. Metre, Dept. of Computer


90
Engg. ioe, nashik
Statement-Level
Control Structures

Dr. K. V. Metre, Dept. of Computer


91
Engg. ioe, nashik
Controlling Program Flows
• Computations in imperative-language programs
– Evaluating expressions – reading variables, executing
operations
– Assigning resulting values to variables
– Selecting among alternative control flow paths
– Causing repeated execution
• A control structure is a control statement and the
statements whose execution it controls
• Most programming languages follow a single
thread of control (or scheduling)
Dr. K. V. Metre, Dept. of Computer
92
Engg. ioe, nashik
Overview
• Selection Statements (Sec. 8.2)
• Iterative Statements (Sec. 8.3)
• Unconditional Branching (Sec. 8.4)
• Guarded Commands (Sec. 8.5)

Dr. K. V. Metre, Dept. of Computer


93
Engg. ioe, nashik
Selection Statements
• A selection statement chooses between two
or more paths of execution
• Two general categories:
– Two-way selectors
– Multiple-way selectors

Dr. K. V. Metre, Dept. of Computer


94
Engg. ioe, nashik
Two-Way Selection Statements
• General form:
if control_expression
then clause
else clause
• Control expression:
– In C89, C99, Python, and C++, the control
expression can be arithmetic
– In languages such as Ada, Java, Ruby, and C#, the
control expression must be Boolean
Dr. K. V. Metre, Dept. of Computer
95
Engg. ioe, nashik
Then and Else Clauses
• In contemporary languages, then and else
clauses can be single or compound statements
– In Perl, all clauses must be delimited by braces
(they must be compound even if there is only 1
statement)
– Python uses indentation to define clauses
if x > y :
x = y
print "case 1"
Dr. K. V. Metre, Dept. of Computer
96
Engg. ioe, nashik
Nesting Selectors
• Consider the following Java code:
if (sum == 0)
if (count == 0)
result = 0;
else result = 1;

• Which if gets the else? (dangling else)


• Java's static semantics rule: else matches with
the nearest if
Dr. K. V. Metre, Dept. of Computer
97
Engg. ioe, nashik
Nesting Selectors (cont.)
• To force an alternative semantics, compound
statements may be used:
if (sum == 0) {
if (count == 0)
result = 0;
}
else result = 1;
• The above solution is used in C, C++, and C#
• Perl requires that all then and else clauses to
be compound and avoid the above problem
Dr. K. V. Metre, Dept. of Computer
98
Engg. ioe, nashik
Nesting Selectors (cont.)
• The problem can also be solved by alternative
means of forming compound statements, e.g.,
using a special word end in Ruby
if sum == 0 then if sum == 0 then
if count == 0 then if count == 0 then
result = 0 result = 0
else end
result = 1 else
end result = 1
end end
Dr. K. V. Metre, Dept. of Computer
99
Engg. ioe, nashik
Multiple-Way Selection Statements
• Allow the selection of one of any number of
statements or statement groups
• Switch in C, C++, Java:
switch (expression) {
case const_expr_1: stmt_1;

case const_expr_n: stmt_n;
[default: stmt_n+1]
}

Dr. K. V. Metre, Dept. of Computer


100
Engg. ioe, nashik
Switch in C, C++, Jave
• Design choices for C’s switch statement
– Control expression can be only an integer type
– Selectable segments can be statement sequences,
blocks, or compound statements
– Any number of segments can be executed in one
execution of the construct (there is no implicit branch
at the end of selectable segments); break is used for
exiting switch  reliability of missing break
– default clause is for unrepresented values (if there
is no default, the whole statement does nothing)
Dr. K. V. Metre, Dept. of Computer
101
Engg. ioe, nashik
Switch in C, C++, Jave

switch (x)
default:
if (prime(x))
case 2: case 3: case 5: case 7:
process_prime(x);
else
case 4: case 6: case 8:
case 9: case 10:
process_composite(x);

Dr. K. V. Metre, Dept. of Computer


102
Engg. ioe, nashik
Multiple-Way Selection in C#
• It has a static semantics rule that disallows the implicit
execution of more than one segment
– Each selectable segment must end with an unconditional
branch (goto or break)
• The control expression and the case constants can be
strings
switch (value) {
case -1: Negatives++; break;
case 0: Zeros++; goto case 1;
case 1: Positives++; break;
default: Console.WriteLine(“!!!\n”); }

Dr. K. V. Metre, Dept. of Computer


103
Engg. ioe, nashik
Multiple-Way Selection in Ada
• Ada
case expression is
when choice list => stmt_sequence;

when choice list => stmt_sequence;
when others => stmt_sequence;]
end case;
• More reliable than C’s switch
– Once a stmt_sequence execution is completed,
control is passed to the first statement after the case
statement

Dr. K. V. Metre, Dept. of Computer


104
Engg. ioe, nashik
Multiple-Way Selection Using if
• Multiple selectors can appear as direct extensions
to two-way selectors, using else-if clauses,
for example in Python:

if count < 10 : More readable than


bag1 = True deeply nested two-
elif count < 100 : way selectors!
bag2 = True
elif count < 1000 : Can compare ranges
bag3 = True
Dr. K. V. Metre, Dept. of Computer
105
Engg. ioe, nashik
Overview
• Selection Statements (Sec. 8.2)
• Iterative Statements (Sec. 8.3)
• Unconditional Branching (Sec. 8.4)
• Guarded Commands (Sec. 8.5)

Dr. K. V. Metre, Dept. of Computer


106
Engg. ioe, nashik
Iterative Statements
• The repeated execution of a statement or
compound statement is accomplished either by
iteration or recursion
• Counter-controlled loops:
– A counting iterative statement has a loop variable,
and a means of specifying the loop parameters: initial,
terminal, stepsize values
– Design Issues:
• What are the type and scope of the loop variable?
• Should it be legal for the loop variable or loop parameters to
be changed in the loop body?

Dr. K. V. Metre, Dept. of Computer


107
Engg. ioe, nashik
Iterative Statements: C-based
for ([expr_1] ; [expr_2] ; [expr_3])
statement
• The expressions can be whole statements or
statement sequences, separated by commas
– The value of a multiple-statement expression is the
value of the last statement in the expression
– If second expression is absent, it is an infinite loop
• Design choices:
– No explicit loop variable  the loop needs not count
– Everything can be changed in the loop
– 1st expr evaluated once, others with each iteration

Dr. K. V. Metre, Dept. of Computer


108
Engg. ioe, nashik
Iterative Statements: C-based
for (count1 = 0, count2 = 1.0;
count1 <= 10 && count2 <= 100.0;
sum = ++count1 + count2, count2 *= 2);
• C++ differs from earlier C in two ways:
– The control expression can also be Boolean
– Initial expression can include variable definitions
(scope is from the definition to the end of loop body)
• Java and C#
– Differs from C++ in that the control expression must
be Boolean

Dr. K. V. Metre, Dept. of Computer


109
Engg. ioe, nashik
Logically-Controlled Loops
• Repetition control based on Boolean expression
• C and C++ have both pretest and posttest forms,
and control expression can be arithmetic:
while (ctrl_expr) do
loop body loop body
while (ctrl_expr)
• Java is like C, except control expression must be
Boolean (and the body can only be entered at the
beginning -- Java has no goto)

Dr. K. V. Metre, Dept. of Computer


110
Engg. ioe, nashik
User-Located Loop Control
• Programmers decide a location for loop control
(other than top or bottom of the loop)
• Simple design for single loops (e.g., break)
• C , C++, Python, Ruby, C# have unconditional
unlabeled exits (break), and an unlabeled
control statement, continue, that skips the
remainder of current iteration, but not the loop
• Java and Perl have unconditional labeled exits
(break in Java, last in Perl) and labeled
versions of continue
Dr. K. V. Metre, Dept. of Computer
111
Engg. ioe, nashik
User-Located Loop Control
• In Java:
outerLoop:
for (row = 0; row < numRows; row++)
for (col = 0; col < numCols; col++)
{ sum += mat[row][col];
if (sum > 1000.0)
break outerLoop;
}

Dr. K. V. Metre, Dept. of Computer


112
Engg. ioe, nashik
Iteration Based on Data Structures
• Number of elements in a data structure control
loop iteration
• Control mechanism is a call to an iterator
function that returns the next element in the data
structure in some chosen order, if there is one;
else loop is terminated
• C's for statement can be used to build a user-
defined iterator:
for(p=root; p==NULL;
traverse(p)){}
Dr. K. V. Metre, Dept. of Computer
113
Engg. ioe, nashik
Iteration Based on Data Structures
• PHP:
reset $list;
print(“1st: “+current($list) + “<br
/>”);
while($current_value = next($list))
print(“next: “+$current_value+”<br
/>”);
• Java 5.0 (uses for, although called foreach)
– For arrays and any other class that implements
Iterable interface, e.g., ArrayList
for (String myElement : myList) { … }

Dr. K. V. Metre, Dept. of Computer


114
Engg. ioe, nashik
Summary
• Variety of statement-level structures
• Choice of control statements beyond selection
and logical pretest loops is a trade-off
between language size and writability
• Functional and logic programming languages
are quite different control structures

Dr. K. V. Metre, Dept. of Computer


115
Engg. ioe, nashik
Subprograms

Dr. K. V. Metre, Dept. of Computer


116
Engg. ioe, nashik
Fundamentals of Subprograms
• Suppose in a program the same sequence of code
appears in many different locations
– Instead of many duplicated codes, we can use only
one copy and jump to and back from that copy of
code from those different locations in the program
• Subprogram:
– Each subprogram has a single entry point
– The calling subprogram is suspended during execution
of the called subprogram
– Control always returns to the caller when the called
subprogram’s execution terminates

Dr. K. V. Metre, Dept. of Computer


117
Engg. ioe, nashik
Fundamentals of Subprograms
• Suppose in a program the same sequence of
code appears in many different locations, and
they differ only in some variables used
– We can use subprograms with parameters
• We can almost always substitute subprogram
code to the location where it is called to
understand what the code looks like there
• Concept of subprogram evolves to produce
things like threads
Dr. K. V. Metre, Dept. of Computer
118
Engg. ioe, nashik
Basic Definitions
• A subprogram definition describes the interface
to and actions of the subprogram
– A subprogram header is the 1st part of the definition,
including name, subprogram kind, formal parameters
void adder(parameters)
– Number, order, types of formal parameters are called
parameter profile (signature) of the subprogram
• A subprogram declaration provides parameter
profile and return type, but not the body
– Prototypes in C and C++; for compilers to see
• A subprogram call jumps to the subprogram
Dr. K. V. Metre, Dept. of Computer
119
Engg. ioe, nashik
Parameters
• Two ways for a subprogram to gain access to the
data for it to process
– Direct access to non-local variables
– Parameter passing
• A formal parameter is a dummy variable listed in
subprogram header and used in the subprogram
– Usually bound to storage when subprogram is called
• An actual parameter represents a value or
address used in the subprogram call statement
Dr. K. V. Metre, Dept. of Computer
120
Engg. ioe, nashik
Binding Actual to Formal Parameter
• Positional parameters
– Binding by position: the first actual parameter is
bound to the first formal parameter and so forth
• Keyword parameters: for long parameter list
– Name of formal parameter to which an actual
parameter is to be bound is specified
sumer(my_length, list=my_array,
sum = my_sum) --
Python
– Pros: Parameters can appear in any order
– Cons: User must know the formal parameter’s names

Dr. K. V. Metre, Dept. of Computer


121
Engg. ioe, nashik
Formal Parameter Default Values
• In certain languages (e.g., C++, Python, Ruby, Ada, PHP),
formal parameters can have default values (if no actual
parameter is passed)
– In C++, default parameters must appear last because
parameters are positionally associated
float comput_pay (float income,
float tax_rate, int exemptions = 1)
pay = comput_pay (20000.0, 0.15);
-- exemptions takes 1
• Variable numbers of parameters
– printf in C

Dr. K. V. Metre, Dept. of Computer


122
Engg. ioe, nashik
Two Categories of Subprograms
• Procedures: collection of statements that define
parameterized computations
• Functions: structurally resemble procedures but
are semantically modeled on math functions
– Should be no side effects and return only one value to
mimic mathematic functions
– In practice, program functions have side effects
– Functions define new user-defined operators
float power(float base, float exp)
result = 3.4 * power(10.0, x);
c.f. result = 3.4 * 10.0 ** x; (Perl)

Dr. K. V. Metre, Dept. of Computer


123
Engg. ioe, nashik
Semantic Models of Param Passing

Dr. K. V. Metre, Dept. of Computer


124
Engg. ioe, nashik
Pass-by-Value (In Mode)
• The value of the actual parameter is used to
initialize the corresponding formal parameter
– Normally implemented by copying
• Disadvantages:
– If by physical move: additional storage is required
(stored twice) and the actual move can be costly
(for large parameters)
– If by access path: must write-protect in callee and
accesses cost more (indirect addressing)
Dr. K. V. Metre, Dept. of Computer
125
Engg. ioe, nashik
Pass-by-Result (Out Mode)
• When a parameter is passed by result, no value is
transmitted to the subprogram; the
corresponding formal parameter acts as a local
variable; its value is transmitted to caller’s actual
parameter when control is returned to the caller,
by physical moving/copying
– Require extra storage location and copy operation
• Potential problem: sub(p1, p1)
– With the two corresponding formal parameters having
different names, whichever formal parameter is
copied back last will represent current value of p1

Dr. K. V. Metre, Dept. of Computer


126
Engg. ioe, nashik
Pass-by-Value-Result (Inout Mode)
• A combination of pass-by-value and pass-by-
result, sometimes called pass-by-copy
– The value of the actual parameter is used to
initialize the corresponding formal parameter,
which then acts as a local variable
• Disadvantages:
– Those of pass-by-result and pass-by-value

Dr. K. V. Metre, Dept. of Computer


127
Engg. ioe, nashik
Pass-by-Reference (Inout Mode)
• Pass an access path, also called pass-by-sharing
– The called subprogram is allowed to access the actual
parameter in the calling subprogram unit
• Advantage:
– Passing process is efficient (no copying and no
duplicated storage)
• Disadvantages
– Slower accesses (compared to pass-by-value) to
formal parameters due to indirect addressing
– Potentials for unwanted changes to actual param.
– Unwanted aliases (access to non-local)

Dr. K. V. Metre, Dept. of Computer


128
Engg. ioe, nashik
Pass-by-Reference (Inout Mode)
• Aliases due to pass-by-reference:
– Collisions between actual parameters, e.g., in C++
void fun(int &first, int &second)
fun(total, total);
– Collisions between array and array elements
fun(list[i], list);
– Collisions between formal parameters and nonlocal
variables that are visible
int *global;
void main() { ... sub(global); ...}
void sub(int *param) {...}
global and param are aliases inside sub()
Dr. K. V. Metre, Dept. of Computer
129
Engg. ioe, nashik
Parameter Passing Methods
• In most languages, parameter communication
takes place thru the run-time stack
– Pass-by-reference are the simplest to implement; only
an address is placed in stack
• C
– Pass-by-value
– Pass-by-reference is achieved by using pointers
• Java
– All parameters are passed by value
– Object parameters are passed by reference
Dr. K. V. Metre, Dept. of Computer
130
Engg. ioe, nashik
Type Checking Parameters
• Very important for reliability
• FORTRAN 77 and original C: none
• Pascal, FORTRAN 90, Java, Ada: required
• ANSI C and C++: choice is made by the user
– Prototypes
• In Python and Ruby, variables do not have
types (objects do), so parameter type
checking is not possible
Dr. K. V. Metre, Dept. of Computer
131
Engg. ioe, nashik
Multidimensional Arrays as Param
• If a multidimensional array is passed to a
subprogram and the subprogram is separately
compiled, compiler needs to know declared
size of that array to build storage mapping
function
– A storage-mapping function for row-major
matrices:
address(mat[i,j])=
address(mat[0,0]) + i * #_columns + j
• Only needs to know #_columns
Dr. K. V. Metre, Dept. of Computer
132
Engg. ioe, nashik
Multidimensional Arrays as Param
• For C and C++:
– Programmer is required to include the declared sizes
of all but the first subscript in the actual parameter
void fun(int mat[][10]) { ... }
void main() {
int mat[5][10]; ...
fun(mat); ... }
– Disallows writing flexible subprograms
– Solution: pass a pointer to the array and sizes of the
dimensions as other parameters; user must include
storage mapping function in terms of size parameters

Dr. K. V. Metre, Dept. of Computer


133
Engg. ioe, nashik
Multidimensional Arrays as Param
• Java and C#
– Arrays are objects; they are all single-dimensioned,
but the elements can be arrays
– Each array inherits a named constant (length in Java,
Length in C#) that is set to the length of the array
when the array object is created, e.g., in Java
float sumer(float mat[][]) {
for(int row=0; row<mat.length; row++)
for(int col=0; col<mat[row].length;
col++)
sum += mat[row][col];

Dr. K. V. Metre, Dept. of Computer


134
Engg. ioe, nashik
Design Considerations
• Two important considerations
– Efficiency
– One-way or two-way data transfer
• But the above considerations are in conflict
– Good programming suggests limited access to
variables, which means one-way whenever
possible
– But pass-by-reference is more efficient to pass
structures of significant size
Dr. K. V. Metre, Dept. of Computer
135
Engg. ioe, nashik
Overview
• Fundamentals of Subprograms (Sec. 9.2 – 9.4)
• Parameter-Passing Methods (Sec. 9.5)
• Parameters That Are Subprograms (Sec. 9.6)
• Overloaded, Generic Subprograms (Sec.
9.7,9.8)
• Design Issues for Functions (Sec. 9.9)
• User-Defined Overloaded Operators (Sec. 9.10)
• Coroutines (Sec. 9.11)

Dr. K. V. Metre, Dept. of Computer


136
Engg. ioe, nashik
Summary
• A subprogram definition describes the actions
represented by the subprogram
• Subprograms can be functions or procedures
• Local variables in subprograms can be stack-
dynamic or static
• Three models of parameter passing: in mode,
out mode, and inout mode
• A coroutine is a special subprogram with
multiple entries
Dr. K. V. Metre, Dept. of Computer
137
Engg. ioe, nashik
Abstract Data Types and Encapsulation
Concepts
What is object-oriented paradigm?

Dr. K. V. Metre, Dept. of Computer


138
Engg. ioe, nashik
A Simple Shooting Game

Dr. K. V. Metre, Dept. of Computer


139
Engg. ioe, nashik
Object-Oriented Programming
• Think from the perspectives of data (“things”)
and their interactions with the external world
– Object
• Data
• Method: interface and message
– Class
• The need to handle similar “things”
– American, French, Chinese, Korean  abstraction
– Chinese: northerners, southerners  inheritance
– Dynamic binding, polymorphism

Dr. K. V. Metre, Dept. of Computer


140
Engg. ioe, nashik
What OOP Allows You?
• You analyze the objects with which you are
working (attributes and tasks on them)
• You pass messages to objects, requesting them to
take action
• The same message works differently when
applied to the various objects
• A method can work with different types of data,
without the need for separate method names
• Objects can inherit traits of previously created
objects
• Information can be hidden better
(Object-Oriented
Dr. K. V. Metre, Programming Using C++)
Dept. of Computer
141
Engg. ioe, nashik
Outline
• The Concept of Abstraction
• Introduction to Data Abstraction
• Design Issues
• Language Examples
• Parameterized Abstract Data Types
• Encapsulation Constructs
• Naming Encapsulations

Dr. K. V. Metre, Dept. of Computer


142
Engg. ioe, nashik
Abstraction
• Two types of abstractions:
– Process abstraction: subprograms
– Data abstraction
• Floating-point data type as data abstraction
– The programming language will provide (1) a way of
creating variables of the floating-point data type, and
(2) a set of operators for manipulating variables
– Abstract away and hide the information of how the
floating-point number is presented and stored
• Need to allow programmers to do the same
– Allow them to specify the data and the operators

Dr. K. V. Metre, Dept. of Computer


143
Engg. ioe, nashik
Abstraction Data Type
• Abstract data type: a user-defined data type
– Declaration of the type and protocol of operations on
objects of the type, i.e., type’s interface, are defined in
a syntactic unit; interface indep. of implementation
– Representation of objects of the type is hidden from
program units that use these objects; only possible
operations are those provided in type's definition
class data type int
object variable i, j, k
method operators
y = stack1.top()+3; vs y = (-x) + +, 3;
-, *, /
Dr. K. V. Metre, Dept. of Computer
144
Engg. ioe, nashik
Advantages of Data Abstraction
• Advantage of having interface independent of
object representation or implementation of
operations:
– Program organization, modifiability (everything
associated with a data structure is together), separate
compilation
• Advantage of 2nd condition (info. hiding)
– Reliability: By hiding data representations, user code
cannot directly access objects of the type or depend
on the representation, allowing the representation to
be changed without affecting user code
Dr. K. V. Metre, Dept. of Computer
145
Engg. ioe, nashik
Language Requirements for ADTs
• A syntactic unit to encapsulate type definition
• A method of making type names and subprogram
headers visible to clients, while hiding actual
definitions
• Some primitive operations that are built into the
language processor
• Example: an abstract data type for stack
– create(stack), destroy(stack), empty(stack), push(stack,
element), pop(stack), top(stack)
– Stack may be implemented with array, linked list, ...
Dr. K. V. Metre, Dept. of Computer
146
Engg. ioe, nashik
Abstract Data Types in C++
• Based on C struct type and Simula 67 classes
• The class is the encapsulation device
– All of the class instances of a class share a single copy
of the member functions
– Each instance has own copy of class data members
– Instances can be static, stack dynamic, heap dynamic
• Information hiding
– Private clause for hidden entities
– Public clause for interface entities
– Protected clause for inheritance (Chapter 12)
Dr. K. V. Metre, Dept. of Computer
147
Engg. ioe, nashik
Member Functions Defined in Class
class Stack {
private:
int *stackPtr, maxLen, topPtr;
public:
Stack() { // a constructor
stackPtr = new int [100];
maxLen = 99; topPtr = -1; };
~Stack () {delete [] stackPtr;};
void push (int num) {…};
void pop () {…};
int top () {…};
int empty () {…}; Implicitly inlined  code
} placed in caller’s code

Dr. K. V. Metre, Dept. of Computer


148
Engg. ioe, nashik
Language Examples: C++ (cont.)
• Constructors:
– Functions to initialize the data members of
instances (they do not create the objects)
– May also allocate storage if part of the object is
heap-dynamic
– Can include parameters to provide
parameterization of the objects
– Implicitly called when an instance is created
– Can be explicitly called
– Name is the same as the class name
Dr. K. V. Metre, Dept. of Computer
149
Engg. ioe, nashik
Language Examples: C++ (cont.)
• Destructors
– Functions to clean up after an instance is destroyed;
usually just to reclaim heap storage
– Implicitly called when the object’s lifetime ends
– Can be explicitly called
– Name is the class name, preceded by a tilde (~)
• Friend functions or classes: to allow access to
private members to some unrelated units or
functions (see Section 11.6.4)
– Necessary in C++

Dr. K. V. Metre, Dept. of Computer


150
Engg. ioe, nashik
Uses of the Stack Class
void main()
{
int topOne;
Stack stk; //create an instance of
the Stack class
stk.push(42); // c.f., stk += 42
stk.push(17);
topOne = stk.top(); // c.f., &stk
stk.pop();
...
}

Dr. K. V. Metre, Dept. of Computer


151
Engg. ioe, nashik
Member Func. Defined Separately
// Stack.h - header file for Stack class
class Stack {
private:
int *stackPtr, maxLen, topPtr;
public:
Stack(); //** A constructor
~Stack(); //** A destructor
void push(int);
void pop();
int top();
int empty();
}

Dr. K. V. Metre, Dept. of Computer


152
Engg. ioe, nashik
Member Func. Defined Separately
// Stack.cpp - implementation for Stack
#include <iostream.h>
#include "Stack.h"
using std::cout;
Stack::Stack() { //** A constructor
stackPtr = new int [100];
maxLen = 99; topPtr = -1;}
Stack::~Stack() {delete[] stackPtr;};
void Stack::push(int number) {
if (topPtr == maxLen)
cerr << "Error in push--stack is full\n";
else stackPtr[++topPtr] = number;}
...

Dr. K. V. Metre, Dept. of Computer


153
Engg. ioe, nashik
Abstract Data Types in Java
• Similar to C++, except:
– All user-defined types are classes
• All objects are allocated from the heap and accessed
through reference variables
– Methods must be defined completely in a class
 an abstract data type in Java is defined and
declared in a single syntactic unit
– Individual entities in classes have access control
modifiers (private or public), rather than clauses
– No destructor  implicit garbage collection

Dr. K. V. Metre, Dept. of Computer


154
Engg. ioe, nashik
An Example in Java
class StackClass {
private int [] stackRef;
private int maxLen, topIndex;
public StackClass() { // a constructor
stackRef = new int [100];
maxLen = 99; topPtr = -1;};
public void push (int num) {…};
public void pop () {…};
public int top () {…};
public boolean empty () {…};
}
Dr. K. V. Metre, Dept. of Computer
155
Engg. ioe, nashik
An Example in Java
public class TstStack {
public static void main(String[] args) {
StackClass myStack = new StackClass();
myStack.push(42);
myStack.push(29);
System.out.println(“:“+myStack.top());
myStack.pop();
myStack.empty();
}
}

Dr. K. V. Metre, Dept. of Computer


156
Engg. ioe, nashik
“Hello World!” Compared
C C++
#include <stdio.h> #include <iostream>
int main(void){ using namespace std;
print("Hello world!"); int main(){
} cout<<"Hello World!"<<endl;
}

Java Ruby
public class HelloWorld { puts 'Hello, world!'
public static void or
main(String[] args){ class String
System.out.println def say
("Hello world!"); puts self
} end
} end
'Hello, world!'.say
Dr. K. V. Metre, Dept. of Computer
157
Engg. ioe, nashik
(http://en.wikibooks.org/wiki/Hello_world_program)
Parameterized ADTs
• Parameterized abstract data types allow
designing an ADT that can store any type
elements (among other things): only an issue
for static typed languages
• Also known as generic classes
• C++, Ada, Java 5.0, and C# 2005 provide
support for parameterized ADTs

Dr. K. V. Metre, Dept. of Computer


158
Engg. ioe, nashik
Parameterized ADTs in C++
• Make Stack class generic in stack size by writing
parameterized constructor function
class Stack {
...
Stack (int size) {
stk_ptr = new int [size];
max_len = size - 1; top = -1; };
...
}

Stack stk(150);

Dr. K. V. Metre, Dept. of Computer


159
Engg. ioe, nashik
Parameterized ADTs in C++ (cont.)
• Parameterize element type by templated class
template <class Type>
class Stack {
private:
Type *stackPtr;
int maxLen, topPtr;
public:
Stack(int size) {
stackPtr = new Type[size];
maxLen = size-1; topPtr = -1; }
...
Stack<double> stk(150);
Instantiated by compiler
Dr. K. V. Metre, Dept. of Computer
160
Engg. ioe, nashik
Generalized Encapsulation
• Enclosure for an abstract data type defines a
SINGLE data type and its operations
• How about defining a more generalized
encapsulation construct that can define any
number of entries/types, any of which can be
selectively specified to be visible outside the
enclosing unit
– Abstract data type is thus a special case

Dr. K. V. Metre, Dept. of Computer


161
Engg. ioe, nashik
Encapsulation Constructs
• Large programs have two special needs:
– Some means of organization, other than simply
division into subprograms
– Some means of partial compilation (compilation units
that are smaller than the whole program)
• Obvious solution: a grouping of logically related
code and data into a unit that can be separately
compiled (compilation units)
• Such collections are called encapsulation
– Example: libraries

Dr. K. V. Metre, Dept. of Computer


162
Engg. ioe, nashik
Means of Encapsulation: Nested
Subprograms
• Organizing programs by nesting subprogram
definitions inside the logically larger
subprograms that use them
• Nested subprograms are supported in Ada,
Fortran 95, Python, and Ruby

Dr. K. V. Metre, Dept. of Computer


163
Engg. ioe, nashik
Encapsulation in C
• Files containing one or more subprograms can be
independently compiled
• The interface is placed in a header file
• Problem:
– The linker does not check types between a header and
associated implementation
• #include preprocessor specification:
– Used to include header files in client programs to
reference to compiled version of implementation file,
which is linked as libraries

Dr. K. V. Metre, Dept. of Computer


164
Engg. ioe, nashik
Encapsulation in C++
• Can define header and code files, similar to those
of C
• Or, classes can be used for encapsulation
– The class header file has only the prototypes of the
member functions
– The member definitions are defined in a separate file
 Separate interface from implementation
• Friends provide a way to grant access to private
members of a class
– Example: vector object multiplied by matrix object
Dr. K. V. Metre, Dept. of Computer
165
Engg. ioe, nashik
Friend Functions in C++
class Matrix;
class Vector {
friend Vector multiply(const Matrix&,
const Vector&);
... }
class Matrix {
friend Vector multiply(const Matrix&,
const Vector&);
... }
Vector multiply(const Matrix& ml,
const Vector& vl) {
... }

Dr. K. V. Metre, Dept. of Computer


166
Engg. ioe, nashik
Naming Encapsulations
• Encapsulation discussed so far is to provide a
way to organize programs into logical units for
separate compilation
• On the other hand, large programs define
many global names; need a way to avoid
name conflicts in libraries and client programs
developed by different programmers
• A naming encapsulation is used to create a
new scope for names

Dr. K. V. Metre, Dept. of Computer


167
Engg. ioe, nashik
Naming Encapsulations (cont.)
• C++ namespaces
– Can place each library in its own namespace and qualify
names used outside with the namespace
namespace MyStack {
... // stack declarations
}
– Can be referenced in three ways:
MyStack::topPtr
using MyStack::topPtr; p = topPtr;
using namespace MyStack; p = topPtr;
– C# also includes namespaces

Dr. K. V. Metre, Dept. of Computer


168
Engg. ioe, nashik
Naming Encapsulations (cont.)
• Java Packages
– Packages can contain more than one class definition;
classes in a package are partial friends
– Clients of a package can use fully qualified name, e.g.,
myStack.topPtr, or use import declaration, e.g.,
import myStack.*;
• Ada Packages
– Packages are defined in hierarchies which correspond
to file hierarchies
– Visibility from a program unit is gained with the with
clause

Dr. K. V. Metre, Dept. of Computer


169
Engg. ioe, nashik
Naming Encapsulations (cont.)
• Ruby classes are name encapsulations, but Ruby
also has modules
• Module:
– Encapsulate libraries of related constants and
methods, whose names in a separate namespace
– Unlike classes  cannot be instantiated or subclassed,
and they cannot define variables
– Methods defined in a module must include the
module’s name
– Access to the contents of a module is requested with
the require method

Dr. K. V. Metre, Dept. of Computer


170
Engg. ioe, nashik
Ruby Modules
module MyStuff
PI = 3.1415
def MyStuff.mymethod1(p1)
...
end
def MyStuff.mymethod(p2)
...
end
end
Require ‘myStuffMod’
myStuff.mymethod1(x)
Dr. K. V. Metre, Dept. of Computer
171
Engg. ioe, nashik
Summary
• Concept of ADTs and the use in program design
was a milestone in languages development
– Two primary features are packaging of data with their
associated operations and information hiding
• C++ data abstraction is provided by classes
• Java’s data abstraction is similar to C++
• Ada, C++, Java 5.0, and C# 2005 support
parameterized ADTs
• C++, C#, Java, Ada, and Ruby provide naming
encapsulations

Dr. K. V. Metre, Dept. of Computer


172
Engg. ioe, nashik
• http://www.cs.nthu.edu.tw/~king/courses/cs2
403.html

Dr. K. V. Metre, Dept. of Computer


173
Engg. ioe, nashik

You might also like