Title: Data Types
1Data Types
- Jianhua Yang
- Department of Math and Computer Science
- Bennett College
2Outline
- Primitive data types
- Character string types
- User-defined ordinal types
- Array types
- Associative arrays
- Record types
- Union types
- Pointer and reference types
36.1 Introduction
- Data type
- Match real-world problems
- Descriptor
Defines a collection of data values and a set of
predefined operations on those values.
Is the collection of the attributes of a variable
Static type Dynamic type
46.2 Primitive Data Types
- Data types that are not defined in terms of other
types are called primitive data types.
51. Numeric types
how to represent negative integer?
- Integer
- Floating-point
- decimal
byte short int long
float double
BCD
Ad precisely Dd range and space
62. Boolean Types
73. Character Types
- ASCII (8 bits)
- Unicode (16 bits)
86.3 Character String Types
- A character string type is one in which the
values consist of sequences of characters.
91. Design issues
- 1. character array, or primitive type?
- 2. static or dynamic length?
102. Strings and their operations
- Different language has different policy
C, and C uses char arrays to store character
strings.
11examples
- char str apples
- char str10apples
- char strapples
12Security problems with string operations
13Solution is
143. String length options
- Static length
- Limited dynamic length
- Dynamic length
C
C
JavaScript, Perl, PHP
154. Evaluation
165. Implementation of Character String Types
Static string Length Address
Dynamic string Current length Address
Limited dynamic string Maximum length Current
length Address
176.4 User-defined Ordinal Types
- An ordinal type is one in which the range of
possible values can be easily associated with the
set of positive integers.
181. Enumeration types
- An enumeration type is one in which all of the
possible values, which are named constants, are
provided in the definition.
19example
- In C, C, C
- enum days Mon, Tue, Wed, Thu, Fri, Sat, Sun
201) Design issues
- Why do we need enum type?
In Fortran 77, we dont have enum type, we
use INTEGER RED, BLUE DATA RED, BLUE /0, 1/
The problems are 1. Operation problem 2.
assign problem.
21example
- In C, we have
- enum colors red, blue, green, yellow, black
- colors myColorblue, yourColorred
-
- myColor
- myColor4 ?
Should be myColor (colors) 4
222) Evaluation
- Enumeration types can provide advantages in both
readability and reliability.
23Reliability
- In Ada, C, and Java
- In C
- In C
- No arithmetic operations are legal on enumeration
types - No enumeration variable can be assigned a value
outside its defined range.
Nothing done.
242. Subrange types
- A subrange type is a contiguous subsequence of an
ordinal type.
251) Adas design
- Example
- type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun)
- subtype Weekdays is Days range Mon..Fri
- subtype Index is Integer range 1..100
1. type checking compile time 2. range
checking run time
Day1 Days Day2 Weekdays Day2Day1
262) Evaluation
- Enhance readability
- Reliability is also increased
276.5 Array Types
- An array is a homogeneous aggregate of data
elements in which an individual element is
identified by its position in the aggregate.
281. Design issues
- 1. What types are legal for subscripts?
- 2. Range checked?
- 3. When are subscript ranges bound?
- 4. When does array allocation take place?
- 5. Ragged or rectangular array?
- 6. Initialized?
292. Arrays and Indexes
- Reference array_name index
- Finite mapping
- array_name(subscript_value_list)?element
30C-language
- Use brackets to delimit their array indices
- Element type and subscript type
- No subscript range checking
313. Subscript Bindings and Array Categories
- The binding of the subscript type to an array
variable is static - Dynamic bind subscript value ranges.
32Five categories
- static array
- fixed stack-dynamic array
- Stack-dynamic array
- Fixed heap-dynamic array
- Heap-dynamic array
33Static array
- The subscript ranges are statically bound and
storage allocation is static. - Advantage its efficiency
- Disadvantage inflexible
C, C Static array static int ary100
34Fixed stack-dynamic array
- Subscript ranges are statically bound, but the
allocation is done during execution time. - Ad. space efficiency
- Da. execution inefficiency
C, C Fixed stack-dynamic array int ary100
35Stack-dynamic array
- Subscript bind and storage allocation are all
dynamic - Ad. flexibility
- Da. Management complexity
Ada arrays Get(List_Len) Declare List
array (1..List_Len) of Integer
36Fixed heap-dynamic array
- Subscript and storage bindingsdynamic (from
heap, rather than stack) - Once allocated, they are fixed
- The bindings are done after they are requested,
rather than at elaboration time.
C malloc, free C new, delete
37Heap-dynamic array
- The same as the previous one in subscript and
storage bindings, but can be changed during the
arrays lifetime - Ad. flexibility
- Da. management complexity
Perl and JavaScript _at_list(1,2,4,7,10)
_at_list() push(_at_list, 13,17)
384. Array Initialization
- Initialized at its storage allocation time
Fortran 95 Integer, Dimension (3)
List(/0,5,5/)
C, C int List4,5,7,83 char
namefreddie char namesBob, Jake,
Darcie
395. Array Operation
- An array operation is one that operates on an
array as a unit. - Different languages have different operations
APL A B, A x B, A .x B
406. Rectangular and Jagged Arrays
- Rectangular array
- Jagged arrays
Same number of elements in each row or column.
The lengths of the rows need not be the same.
C, C, C, and Java all support Jagged array, as
well as rectangular array.
417. Slices
- A slice of array is some substructure of the
array.
Fortran Integer, Dimension(10) Vector Integer,
Dimension(3,3) Mat Integer, Dimension(3,3,4)Cub
e Slices Vector(36), Mat(, 2), Mat(3,),
Cube(,,2)
C, C char name3Johnson, Harriton,
Ponting Slices name1, name2
428. Implementation of Array Types
- Single dimensional array
- Multiple dimensional array
43Single dimensional array
element type Index type index lower
bound index upper bound address
map to a list of adjacent memory
44Multiple dimensional array
element type Index type number of
dimensions index 1 range index 2 range
index n range address
Row major mapping Column major mapping
456.6 Associative Arrays
- Hash function
- Study by yourself
466.7 Record Type
- A record is a possible heterogeneous aggregate of
data elements.
471. Definition of records
- To define each field of a record.
In C, C struct Student_type char name
int grade float average char
addr int enroll_status Student
482. References to Record Fields
- Student1.name Student10.average
493. Operations
- Depend on the Language itself
504. Implementation
Record Name Type Offset Name Type Offset Addres
s
Field 1
Field n
516.8 Union Type
526.9 Pointer Type
- A pointer type is one in which the variables have
a range of values that consists of memory
addresses and a special value, nil.
- Not structured type
- Used to reference other variables
53Goals
- 1. to provide indirect address
- 2. to provide a way to manage dynamic storage.
541. Designed issues
- What are the scope 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?
552. Pointer operations
- Example in C, C
- int ptr
- jptr
7080
ptr
206
j
8951
563. Pointers in C, and C
- Look at the following example
int ptr int count, init ptrinit countptr
countinit
57Access array in C, C
- int list10
- int ptr
- ptrlist
- (ptr1), (ptr2), can be used to access the
elements of list.
584. Pointer problems
- 1) Dangling problems
- 2) Lost heap-dynamic variables
59Dangling Pointers
- It is a pointer that contains the address of a
heap-dynamic variable that has been deallocated.
C examples int arrayPtr1 int arrayPtr2new
int100 arrayPtr1arrayPtr2 delete
arraryPtr2
60Lost Heap-Dynamic variables
- It is an allocated heap-dynamic variable that is
no longer accessible to the user program.
Also called garbage
The reason is that the pointer points to the
variable has been set to point to other area.
61Reference types
- This type variables are specified in definitions
by preceding their names with ampersnads (). For
example - int result 0
- int ref_resultresult
-
- ref_result 100
62Summary
- Primitive data types
- User-defined ordinal types
- Array types
- Record types
- Pointer types