Chapter 6 – Data Types CSCE 343.

Slides:



Advertisements
Similar presentations
Chapter 6 Data Types.
Advertisements

Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
ISBN Lecture 06 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Lecture 06 Topics Introduction Primitive Data.
ISBN Chapter 6 Data Types: Structured types.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Definitions A data type defines a collection of data.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
Primitive Data Types: Numbers Strings Ordinal Types Pointers
Lecture 6 Concepts of Programming Languages
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
CS 355 – Programming Languages
ISBN 0-321— Chapter 6 Data Types. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
College of Computer Science and Engineering
ISBN 0-321— Chapter 6 Data Types. Corrected and improved by Assoc. Prof. Zeki Bayram, EMU, North Cyprus. Original Copyright © 2007 Addison-Wesley.
ISBN 0-321— Chapter 6 sections 1-4, 9 Primitive Data Types Numbers Strings Ordinal Types Pointers.
1 Data Types In Text: Chapter 5. 2 Chapter 5: Data Types Outline What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers.
6-1 Chapter 6: Data Types Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types.
ISBN 0-321— Chapter 6 Structured Data Types Arrays Associated Arrays Records Unions.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN Chapter 6 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Chapter 6 Topics Introduction Primitive Data.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
Structure of Programming Language Data Types. 2 A data type defines a collection of data objects and a set of predefined operations on those objects An.
C H A P T E R S I X Data Types.
1 CS Programming Languages Class 08 September 19, 2000.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved. 6-2 Chapter 6 Topics Introduction Primitive Data Types.
Data Types W E E K F O U R. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character String.
Chapter 6 © 2002 by Addison Wesley Longman, Inc Introduction - Evolution of Data Types: FORTRAN I (1957) - INTEGER, REAL, arrays … Ada (1983) -
CS 330 Programming Languages 11 / 01 / 2007 Instructor: Michael Eckmann.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.2 Primitive Data Types Almost all programming languages.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 5 Evolution of Data Types: FORTRAN I (1956) - INTEGER, REAL, arrays … Ada (1983) - User can.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
Structure of Programming Language Data Types. 2 A data type defines a collection of data objects and a set of predefined operations on those objects An.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Data Types Chapter 6: Data Types Lectures # 11. Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record.
CSC 533: Programming Languages Spring 2016
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
CSC 533: Organization of Programming Languages Spring 2010
Dr. Vamsi Paruchuri University of Central Arkansas
Chapter 6 Data Types.
Data Types I Subject : T0152 – Programming Language Concept
Chapter 6 Data Types.
Chapter 6: Data Types Saad Alshahrani
CSC 533: Programming Languages Spring 2015
Chapter 6 Data Types.
Concepts of Programming Languages
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
6.1 Introduction 6.2 Primitive Data Types - Evolution of Data Types:
Chapter 6: Data Types Lectures # 10.
Structure of Programming Language
Instructor : Ahmed Alalawi Slides from Chung –Ta King
Concepts of programming languages
Chapter 6 Data Types.
Data Types In Text: Chapter 6.
Complex Data Types One very important measure of the “goodness” of a PL is the capability of its data types to model the problem space variables Design.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Presentation transcript:

Chapter 6 – Data Types CSCE 343

Primitive Types Types not defined in terms of other types double, int, char, etc. Java signed integer types: byte, short, int, long Decimal types: fixed accuracy (C#, COBOL) Boolean type Advantage readability, reliability C: if (x=5){…} Char type Coding: ASCII (8 bit), Unicode (16 bit)

String Types Value: sequence of characters Issues: Typical operations primitive or char array? Static or dynamic length? Typical operations Assignment, comparison, catenation, substring, pattern matching C, C++: char arrays, (string class) SNOBOL4: primitive Java: String class Built in operations (.equals(), + , substring())

String Length Static: set when the string is created COBOL, Java, C++ string, Python, Ruby Limited Dynamic: varying length up to fixed size char arrays in C using \0. Dynamic: varying length no maxium Perl, JavaScript, Java (StringBuilder)

Ordinal Types Type with limited range associated with positive integers: Java: integer, char, boolean Enumeration types Named constants for all values C#: enum days {mon, tue, wed, thu, fri, sat, sun}; enum days x = mon; Aids readability Aids reliability: Compiler checks operations, range

Subrange Types A sub-range of an ordinal type Aid to readability subtype Index is Integer range 0..99 Aid to readability Clear about valid values Reliability Range checks

Array Types Array: aggregate of homogenous data elements Issues: Each element is identified by position Issues: Legal types for subscripts? Range checking? Binding of subscript to ranges. When does array allocation take place? Allow multidimensional and/or ragged arrays Max number of subscripts? Initialization? Slicing?

Indexes Types: Range checking: Index Type FORTRAN, C, C++, Java: integer types only. Pascal: any ordinal type (integer, boolean, char, enumeration) Range checking: C, C++, Perl, FORTRAN: No Java, C#: Yes Perl: Kind of (no error message, returns undef) Index Type static binding

Array Categories Must determine binding of subrange binding memory allocation stack or heap memory Static: a[10] Allocation, subscript range bindings at compile time. (efficient) Fixed stack-dynamic: a[10] Subscript ranges are static, allocation is done at declaration execution (space efficient) Stack dynamic: a[x] Subscript range and storage is done at run-time (declaration) fixed after allocation Fixed heap-dynamic:a[x] Similar to stack dynamic, fixed after allocation (heap) Heap dynamic Everything (range, storage) is dynamic (heap) ArrayList, vector

Examples Static: C,C++ static modifier Fixed stack-dynamic: C, C++ local arrays Stack dynamic: C++ Fixed heap-dynamic: Java Heap-dynamic: Perl, JavaScript foo(){ int a[10]; int b[x]; int c[] = new int[x]; ArrayList d = new ArrayList();

Initialization C, C++, Java, C#: Character strings in C, C++: int list[] = {4,5,7,83}; Character strings in C, C++: char name[] = “Nick”; Arrays of strings in C, C++: char *names[] = {“Bob”, “Jake”, “Jane”}; names[0] is a pointer to ‘B’ (literal char array) Java initialization of String objects: String[] names = {“Bob”, “Jake”, “Joe”}; Java 2D arrays: int[][] a = {{1,2}, {3,4,5}};

Array Implementation Contiguous memory Calculating address: addr(a[i]) = addr(a[0]) + ( i * size ) In general: addr(a[i]) = addr(a[lb]) + ( (i – lb) * size )

2D Array Implementation Contiguous memory: Row-major order (most common) Column-major order (FORTRAN) Address functions: Row major: addr(a[i][j]) = addr(a[0][0]) + (((i * n) + j ) * size)

Array Slicing Extracting parts of arrays: Perl and Python Python example a1 = [0, 1, 2, 3, 4, 5, 6] a2 = a1[3:5] a3 = a1[:4] a4 = a1[3:]

In-class Find the memory address of char a[20][15]; float b[15][20]; Make the following assumptions a is allocated memory starting at byte 800 in memory b is allocated memory starting at byte 1800 in memory a float is 4 bytes two dimensional arrays use row-major order Find the memory address of a[10][5] b[5][10]

Associative Arrays Like the STL map (key, data value) pairs Key is index to data value typical data is stored in an array array component selected with hash(key) Python example

Record Types Record: aggregate of data elements Elements (fields) identified by name Before records parallel arrays were used C: struct, Ada: record, COBOL: records Access to elements with dot (.): x.name Operations: Assignment allowed? Comparison?

Implementation of Record Types Compile time descriptor. Runtime descriptor not needed, since this information is available at compile time.

Union Types Allows different types at different times C,C++, no type checking (free union) Ada includes type checking with discriminant. Evaluation: Increase flexibility Potentially unsafe (type checking) Java, C# do not support unions. Example code for struct and union

Pointer and Reference Types Pointer: value is memory address or NULL Indirect addressing Manage dynamic memory (heap) Design issues: What are scope & lifetime of pointer variables? What is the lifetime of heap-dynamic variable? Are there restrictions as to what a pointer can point to? Are pointers used for dynamic storage management and/or indirect addressing Should the language have pointer types, reference types, or both?

Pointer Operations Assignment and dereferencing int *ptrInt, *array, x=5; //assignment ptrInt = &x; array = malloc(x*sizeof(int)); //dereferencing x = (*ptrInt)+1; a[0] = x;

Pointer Problems Dangling Pointers int *p; { int x; p = &x;} //p points to memory that has been reclaimed *char foo(){char s[20]; return s;} yikes! Lost heap-dynamic variables (memory leak) int *ptrArray, a[30]; ptrArray = malloc(1000* sizeof(int)); ptrArray = a;

Pointers in C, C++ Point at any variable of correct type Used for dynamic storage management and addressing Pointer arithmetic (more interesing?) Explicit dereferencing and address-of operators Domain type need not be fixed (void *) generic pointer cannot be dereferenced

C Reference types Special kind of pointer type Primary use: formal parameters See example code What are advantages and disadvantages of pass-by-reference? Java extends C++ reference variables to replace pointers entirely implicitly dereferenced C# includes both references of Java and pointers of C++

Heap Management Two approaches to garbage collection: Reference counters (eager approach): done incrementally heap cell store count of references to it mark-sweep (lazy approach): done when available cell list is empty heap cell has a “garbage bit” set all garbage bits on check all pointes to turn off garbage bit sweep to recover

Exam 1 review guide