Data Types - PowerPoint PPT Presentation

1 / 62
About This Presentation
Title:

Data Types

Description:

Jagged arrays. Same number of elements in each row or column. ... C, C , C#, and Java all support Jagged array, as well as rectangular array. 7. Slices ... – PowerPoint PPT presentation

Number of Views:647
Avg rating:3.0/5.0
Slides: 63
Provided by: jhy9
Category:
Tags: data | jagged | ragged | types

less

Transcript and Presenter's Notes

Title: Data Types


1
Data Types
  • Jianhua Yang
  • Department of Math and Computer Science
  • Bennett College

2
Outline
  • Primitive data types
  • Character string types
  • User-defined ordinal types
  • Array types
  • Associative arrays
  • Record types
  • Union types
  • Pointer and reference types

3
6.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
4
6.2 Primitive Data Types
  • Data types that are not defined in terms of other
    types are called primitive data types.

5
1. Numeric types
how to represent negative integer?
  • Integer
  • Floating-point
  • decimal

byte short int long
float double
BCD
Ad precisely Dd range and space
6
2. Boolean Types
  • True (1), false (0)

7
3. Character Types
  • ASCII (8 bits)
  • Unicode (16 bits)

8
6.3 Character String Types
  • A character string type is one in which the
    values consist of sequences of characters.

9
1. Design issues
  • 1. character array, or primitive type?
  • 2. static or dynamic length?

10
2. Strings and their operations
  • Different language has different policy

C, and C uses char arrays to store character
strings.
11
examples
  • char str apples
  • char str10apples
  • char strapples

12
Security problems with string operations
  • strcpy(src, dst)

13
Solution is
  • Using C string class.

14
3. String length options
  • Static length
  • Limited dynamic length
  • Dynamic length

C
C
JavaScript, Perl, PHP
15
4. Evaluation
  • Which way is better?

16
5. Implementation of Character String Types
Static string Length Address
Dynamic string Current length Address
Limited dynamic string Maximum length Current
length Address
17
6.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.
  • Enumeration
  • subrange

18
1. Enumeration types
  • An enumeration type is one in which all of the
    possible values, which are named constants, are
    provided in the definition.

19
example
  • In C, C, C
  • enum days Mon, Tue, Wed, Thu, Fri, Sat, Sun

20
1) 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.
21
example
  • In C, we have
  • enum colors red, blue, green, yellow, black
  • colors myColorblue, yourColorred
  • myColor
  • myColor4 ?

Should be myColor (colors) 4
22
2) Evaluation
  • Enumeration types can provide advantages in both
    readability and reliability.

23
Reliability
  • 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.
  • There is a range check

24
2. Subrange types
  • A subrange type is a contiguous subsequence of an
    ordinal type.

25
1) 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
26
2) Evaluation
  • Enhance readability
  • Reliability is also increased

27
6.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.

28
1. 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?

29
2. Arrays and Indexes
  • Reference array_name index
  • Finite mapping
  • array_name(subscript_value_list)?element

30
C-language
  • Use brackets to delimit their array indices
  • Element type and subscript type
  • No subscript range checking

31
3. Subscript Bindings and Array Categories
  • The binding of the subscript type to an array
    variable is static
  • Dynamic bind subscript value ranges.

32
Five categories
  • static array
  • fixed stack-dynamic array
  • Stack-dynamic array
  • Fixed heap-dynamic array
  • Heap-dynamic array

33
Static array
  • The subscript ranges are statically bound and
    storage allocation is static.
  • Advantage its efficiency
  • Disadvantage inflexible

C, C Static array static int ary100
34
Fixed 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
35
Stack-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
36
Fixed 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
37
Heap-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)
38
4. 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
39
5. 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
40
6. 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.
41
7. 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
42
8. Implementation of Array Types
  • Single dimensional array
  • Multiple dimensional array

43
Single dimensional array
  • Descriptor
  • Map to memory

element type Index type index lower
bound index upper bound address
map to a list of adjacent memory
44
Multiple dimensional array
  • Descriptor
  • Mapping

element type Index type number of
dimensions index 1 range index 2 range
index n range address
Row major mapping Column major mapping
45
6.6 Associative Arrays
  • Hash function
  • Study by yourself

46
6.7 Record Type
  • A record is a possible heterogeneous aggregate of
    data elements.

47
1. 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
48
2. References to Record Fields
  • Student1.name Student10.average

49
3. Operations
  • Depend on the Language itself

50
4. Implementation
Record Name Type Offset Name Type Offset Addres
s
Field 1
Field n
51
6.8 Union Type
  • Self study

52
6.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

53
Goals
  • 1. to provide indirect address
  • 2. to provide a way to manage dynamic storage.

54
1. 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?

55
2. Pointer operations
  • Example in C, C
  • int ptr
  • jptr

7080
ptr
206
j
8951
56
3. Pointers in C, and C
  • Look at the following example

int ptr int count, init ptrinit countptr

countinit
57
Access array in C, C
  • int list10
  • int ptr
  • ptrlist
  • (ptr1), (ptr2), can be used to access the
    elements of list.

58
4. Pointer problems
  • 1) Dangling problems
  • 2) Lost heap-dynamic variables

59
Dangling 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
60
Lost 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.
61
Reference 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

62
Summary
  • Primitive data types
  • User-defined ordinal types
  • Array types
  • Record types
  • Pointer types
Write a Comment
User Comments (0)
About PowerShow.com