CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Chapter 6 Data Types
Arrays and records Programming Language Design and Implementation
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Data Types-II: Composite Data Types.
Chapter 7:: Data Types Programming Language Pragmatics
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Data Types Programming languages need a variety of data types in order to better model/match the world more data types make programming easier but too.
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Chapter Four Data Types Pratt 2 Data Objects A run-time grouping of one or more pieces of data in a virtual machine a container for data it can be –system.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
ISBN Chapter 6 Data Types: Structured types.
G. Levine Chapter 6 Chapter 6 Encapsulation –Why do we want encapsulation? Programmer friendliness- programmer need not know about these details –Easier.
Multidimensional Arrays in Java Vidhu S. Kapadia.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Definitions A data type defines a collection of data.
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Elementary Data Types Scalar Data Types Numerical Data Types Other
Structured Data Types and Encapsulation Mechanisms to create new data types: –Structured data Homogeneous: arrays, lists, sets, Non-homogeneous: records.
Arrays. The array data structure An array is an indexed sequence of components –Typically, the array occupies sequential storage locations –The length.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.
Addressing Modes Chapter 11 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer,  S.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
College of Computer Science and Engineering
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
CSI 3125, Names etc., page 1 Names, binding, scope, type checking Names, variables Binding Scope Constants. Variable initialization Type checking Plan.
Arrays. Arrays as ADTs An array is an Abstract Data Type –The array type has a set of values The values are all the possible arrays –The array type has.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,
Basic Semantics Associating meaning with language entities.
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.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
CSI 3120, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
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.
Arrays and Pointers Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 23.
1 CS Programming Languages Class 09 September 21, 2000.
ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.2 Primitive Data Types Almost all programming languages.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Arrays. Arrays as ADTs An array is an Abstract Data Type –The array type has a set of values The values are all the possible arrays –The array type has.
Names, Scope, and Bindings Programming Languages and Paradigms.
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.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
Windows Programming Lecture 03. Pointers and Arrays.
Records type city is record -- Ada Name: String (1..10); Country : String (1..20); Population: integer; Capital : Boolean; end record; struct city { --
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Data Types Chapter 6: Data Types Lectures # 11. Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record.
5.2 Names - We discuss all user-defined names here
5.2 Names - We discuss all user-defined names here
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
Type Checking, and Scopes
Structure of Programming Language
Concepts of programming languages
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.
Arrays.
Chapter 6 Data Types.
Course Overview PART I: overview material PART II: inside a compiler
Types and Related Issues
Presentation transcript:

CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment

CSI 3125, Data Types, page 2 Arrays An array represents a mapping: index_type  component_type The index type must be a discrete type (integer, character, enumeration etc). In some languages this type is specified implicitly: an array of size N is indexed 0…N-1 in C++ / Java / Perl, but in Fortran it is 1…N. In Algol, Pascal, Ada the lower and upper bound must be both given. There are normally few restrictions on the component type (in some languages we can even have arrays of procedures or files).

CSI 3125, Data Types, page 3 Multidimensional arrays Multidimensional arrays can be defined in two ways (for simplicity, we show only dimension 2): index_type1  index_type2  component_type This corresponds to references such as A[I,J]. Algol, Pascal, Ada work like this. index_type1  (index_type2  component_type) This corresponds to references such as A[I][J]. Java works like this. Perl sticks to one dimension

CSI 3125, Data Types, page 4 Operations on arrays (1) select an element (get or change its value): A[J] select a slice of an array: (read the textbook, Section 6.5.7) assign a complete array to a complete array: A := B; There is an implicit loop here.

CSI 3125, Data Types, page 5 Operations on arrays (2) Compute an expression with complete arrays (this is possible in extendible or specialized languages, for example in Ada): V := W + U; If V, W, U are arrays, this may denote array addition. All three arrays must be compatible (the same index and component type), and addition is probably carried out element by element.

CSI 3125, Data Types, page 6 Subscript binding static:fixed size, static allocation this is done in older Fortran. semistatic:fixed size, dynamic allocation Pascal. semidynamic:size determined at run time, dynamic allocation Ada dynamic:size fluctuates during execution, flexible allocation required Algol 68, APL—both little used...

CSI 3125, Data Types, page 7 Array-type constants and initialization Many languages allow initialization of arrays to be specified together with declarations: C int vector [] = {10,20,30}; Ada vector: array(0..2) of integer := (10,20,30); Array constants in Ada temp is array(mo..su)of ; T: temp; T := (15,12,18,22,22,30,22); T := (mo=>15, we=>18, tu=>12, sa=>30, others=>22); T := (15,12,18, sa=>30, others=>22);

CSI 3125, Data Types, page 8 Implementing arrays (1) The only issue is how to store arrays and access their elements—operations on the component type decide how the elements are manipulated. An array is represented during execution by an array descriptor. It tells us about: the index type, the component type, the address of the array, that is, the data.

CSI 3125, Data Types, page 9 Implementing arrays (2) Specifically, we need: the lower and upper bound (for subscript checking), the base address of the array, the size of an element. We also need the subscript—it gives us the offset (from the base) in the memory area allocated to the array. A multi-dimensional array will be represented by a descriptor with more lower-upper bound pairs.

CSI 3125, Data Types, page 10 Implementing multidimensional arrays Row major order (second subscript increases faster) Column major order (first subscript increases faster)

CSI 3125, Data Types, page 11 Suppose that we have this array: A: array [LOW1..HIGH1, LOW2..HIGH2] of ELT; where the size of each entity of type ELT is SIZE. This calculation is done for row-major (calculations for column-major are quite similar). We need the base—for example, the address LOC of A[LOW1, LOW2]. Implementing multidimensional arrays (2)

CSI 3125, Data Types, page 12 We can calculate the address of A[I,J] in the row-major order, given the base. Let the length of each row in the array be: ROWLENGTH = HIGH2 - LOW2 + 1 The address of A[I,J] is: (I - LOW1) * ROWLENGTH * SIZE + (J - LOW2) * SIZE + LOC Implementing multidimensional arrays (3)

CSI 3125, Data Types, page 13 Here is an example. VEC: array [1..10, 5..24] of integer; The length of each row in the array is: ROWLENGTH = = 20 Let the base address be 1000, and let the size of an integer be 4. The address of VEC[i,j] is: (i - 1) * 20 * 4 + (j - 5) * For example, VEC[7,16] is located in 4 bytes at 1524 = (7 - 1) * 20 * 4 + (16 - 5) * Implementing multidimensional arrays (4)

CSI 3125, Data Types, page 14 Languages without arrays A final word on arrays: they are not supported by standard Prolog and pure Scheme. An array can be simulated by a list, which is the basic data structure in Scheme and a very important data structure in Prolog. Assume that the index type is always 1..N. Treat a list of N elements: [x 1, x 2,..., x N ](Prolog) (x 1 x 2... x N )(Scheme) as the (structured) value of an array

CSI 3125, Data Types, page 15 Back to pointers [Note: We’re skipping 6.9.9] A pointer variable has addresses as values (and a special address nil or null for "no value"). They are used primarily to build structures with unpredictable shapes and sizes—lists, trees, graphs—from small fragments allocated dynamically at run time. A pointer to a procedure is possible, but normally we have pointers to data (simple and composite). An address, a value and usually a type of a data item together make up a variable. We call it an anonymous variable: no name is bound to it. Its value is accessed by dereferencing the pointer.

CSI 3125, Data Types, page 16 Note that, as with normal named variables, in this: p^ := 23; we mean the address of p^ (the value of p). In this: m := p^; we mean the value of p^. value(p) =  value(p^) =  p  Back to pointers (2) Pointers in Pascal are quite well designed.

CSI 3125, Data Types, page 17 Pointer variable creation A pointer variable is declared explicitly and has the scope and lifetime as usual. An anonymous variable has no scope (because it has no name) and its lifetime is determined by the programmer. It is created (in a special memory area called heap) by the programmer, for example: new(p); in Pascal p = malloc(4); in C and destroyed by the programmer: dispose(p); in Pascal free(p); in C

CSI 3125, Data Types, page 18 If an anonymous variable exists outside the scope of the explicit pointer variable, we have "garbage" (a lost object). If an anonymous variable has been destroyed inside the scope of the explicit pointer variable, we have a dangling reference. new(p); p^ := 23; dispose(p); if p^ > 0 {???} Pointer variable creation (2)

CSI 3125, Data Types, page 19 Producing garbage, an example in Pascal: new(p); p^ := 23; new(p); {the anonymous variable with the value 23 becomes inaccessible} Garbage collection is the process of reclaiming inaccessible storage. It is usually complex and costly. It is essential in languages whose implementation relies on pointers: Lisp, Prolog. Pointer variable creation (2)

CSI 3125, Data Types, page 20 Pointers: types and operators Pointers in PL/I are typeless. In Pascal, Ada, C they are declared as pointers to types, so that a dereferenced pointer (p^, *p) has a fixed type. Operations on pointers in C are quite rich: char b, c; c = '\007'; b = *((&c - 1) + 1); putchar(b);