Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Types Chapter 6: Data Types Lectures # 11. Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record.

Similar presentations


Presentation on theme: "Data Types Chapter 6: Data Types Lectures # 11. Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record."— Presentation transcript:

1 Data Types Chapter 6: Data Types Lectures # 11

2 Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record Types Union Types Pointer and Reference Types Chapter 6: Data Types 2

3 Array Types An array is a homogeneous aggregate of data elements in which an element is identified by its position in the aggregate, relative to the first element. References to elements include one or more subscripts. References require a runtime computation to determine the location being referenced. Array types are implemented in most programming languages. Chapter 6: Data Types 3

4 Categories of Arrays Five categories of arrays based on subscript range binding and storage binding:  Static.  Fixed stack dynamic.  Stack dynamic.  Fixed heap dynamic.  Heap dynamic. Chapter 6: Data Types 4

5 Categories of Arrays: Static Arrays Static arrays are those in which:  Subscript ranges are statically bound.  Storage bindings are static. Advantage: execution efficiency since no dynamically allocation/deallocation is required. FORTRAN arrays are static. Chapter 6: Data Types 5

6 Categories of Arrays: Fixed Stack-Dynamic Arrays Fixed stack-dynamic arrays are those in which:  Subscript ranges are statically bound.  Allocation is done at declaration elaboration time. Advantage is space efficiency.  Storage is allocated only while block in which array is declared is active. Pascal local arrays. Chapter 6: Data Types 6

7 Categories of Arrays: Stack-dynamic arrays A stack-dynamic array is one in which:  Subscript ranges are dynamically bound.  Storage allocation is done at runtime.  Both remain fixed during the lifetime of the variable. Advantage: flexibility. Ada has this kind of array: get N declare ARR : array(1..N) of INTEGER; begin … end;  In this example the user inputs N. The elements are then dynamically allocated when execution reaches the declare block. Chapter 6: Data Types 7

8 Categories of Arrays: Fixed Heap-dynamic arrays Fixed Heap-dynamic array is similar to fixed stack-dynamic array in which:  Storage binding is dynamic but fixed after allocation. Difference between fixed heap and fixed stack:  Binding is done when requested rather than at elaboration time.  Storage is allocated from heap not stack. C and C++ allow dynamic arrays via:  malloc and free.  new and delete. Chapter 6: Data Types 8

9 Categories of Arrays: Fixed Heap-dynamic arrays (cont.) In Java all arrays are fixed-heap dynamic. C# includes a second array class ArrayList that provides fixed heap-dynamic. Fortran 95 support fixed-heap dynamic. INTEGER ALLOCATABLE, ARRAY(:,:) :: MAT ALLOCATE (MAT(10, 20)) …… DEALLOCATE(MAT(10, 20)) Chapter 6: Data Types 9

10 Categories of Arrays: Heap-dynamic Arrays A heap-dynamic array is one in which:  Subscript range binding is dynamic.  Storage allocation is dynamic.  Either can change any number of times during execution. Arrays can grow and shrink as the need arises. C# provides heap dynamic arrays.  Object of this class is created without any elements as in: ArrayList intList = new arrayList(); Perl and JavaScript also support heap-dynamic arrays. Chapter 6: Data Types 10

11 Array initialization FORTRAN: INTEGER LIST(3) DATA LIST /10, 20, 30/ C, C++: int list[] = {10, 20, 30}; char name[] = “bob”; char* names[] = {“bob”,”sue”,”tom”}; Java: String[]names = {“bob”,”sue”,”tom”}; Ada: LIST: array(1..5) of INTEGER := (1,2,3,4,5); LIST2: array(1..10) of INTGEGER := (1 => 10, 2..5 => 100, others => 0); Chapter 6: Data Types 11

12 Array implementation Compile-time descriptor for a 1 -dimensional array may include:  Element type.  Index type.  Index lower bound.  Index upper bound.  Address. For multi-dimensional arrays, information about each index range must be stored. Chapter 6: Data Types 12

13 Row- vs Column-major order Chapter 6: Data Types 13

14 Associative arrays An associative array is an unordered collection of data elements that are indexed by an equal number of values called keys. The keys are stored in the structure. Thus, element is a (key, value) pair. Design issues:  What is the form of references to elements. Chapter 6: Data Types 14

15 Associative Arrays in Perl Names begin with %; literals are delimited by parentheses %hi_temps = ("Mon" => 77, "Tue" => 79, “Wed” => 65, … );  Subscripting is done using braces and keys: $hi_temps{"Wed"} = 83;  Elements can be removed with delete : delete $hi_temps{"Tue"};  The entire hash can be emptied by assigning the empty literal to it: $hi_temps = (); Chapter 6: Data Types 15


Download ppt "Data Types Chapter 6: Data Types Lectures # 11. Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record."

Similar presentations


Ads by Google