Array Types Index types can be of any discrete type Component type must be definite, i.e. have bounds: type class_list is array ( 1.. 100) of String (1..10);

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Slide: 1 Copyright © AdaCore Arrays Presented Quentin Ochem university.adacore.com.
Names and Bindings.
Chapter 7:: Data Types Programming Language Pragmatics
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Kernighan/Ritchie: Kelley/Pohl:
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,
Subprograms The basic abstraction mechanism. Functions correspond to the mathematical notion of computation input output procedures affect the environment,
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Informática II Prof. Dr. Gustavo Patiño MJ
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
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.
Mark Hennessy CS351 Dept Computer Science NUI Maynooth 1 Types CS351 – Programming Paradigms.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Primitive Data Types: Numbers Strings Ordinal Types Pointers
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Lecture 4 Aggregate Data Types. 2 Aggregates Types defined as aggregates of other types Types defined as aggregates of other types At least two types.
Data Types.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
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.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Basic Semantics Associating meaning with language entities.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Array Types Index types can be of any discrete type Component type must be definite, i.e. have bounds: type class_list is array ( ) of String (1..10);
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.2 Primitive Data Types Almost all programming languages.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Names, Scope, and Bindings Programming Languages and Paradigms.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
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.
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
Records Design Issues: 1. What is the form of references?
Subprograms The basic abstraction mechanism.
CSCE 210 Data Structures and Algorithms
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 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Array Types Index types can be of any discrete type Component type must be definite, i.e. have bounds: type class_list is array ( ) of String (1..10); -- OK type class_list is array ( ) of String; -- Error The subtype constrains all indices or none:: type Matrix is array (positive range <>, positive range <>) of Long_Float; subtype Table is Matrix; subtype Rotation is Matrix (1.. 3, 1.. 3); arrays are objects with assignment: (unlike C, C++) Table1 := Table2; -- all components assigned

Anonymous Array Types Grades : array (1.. Num_Students) of Natural; type of Grades has no name: distinct from any other array types. Ar1: array (1.. 10) of Boolean; Ar2 : array (1.. 10) of Boolean; … Ar1 := Ar2; -- Error: different (anonymous) types. If a type is a useful abstraction, it deserves to have a name!

Array Attributes type Matrix is array (Positive range <>, Positive range <>) of Float; subtype Rect is Matrix (1.. 3, 1.. 5); M3 : Rect; M3’First (1) -- Yields 1 M3’First -- same. Rect’length (2) -- Yields 5 (applies to type) M3’range (2) -- equivalent to 1..5 String’Length -- ERROR: unconstrained Arrays are self-describing: size information is built-in

Array Aggregates Expression that yields an array value: A := (1, 2, 3, 10); -- positional A := (1, others => 0); -- notation for default. A := (1..3 => 1, 4 => -999); -- component associations Default can only be used if bounds are known: A : String (1.. 10) := (others => ‘?’); -- OK A : String := (others => ‘?’); -- Error: unknown bounds.

Initializers in C++ Similar notion for declarations: int v2[] = {1, 2, 3, 4}; -- size from initializer char v3[2] = {‘a’, ‘z’}; -- declared size int v5[10] = {-1}; -- default : other components = 0 char name [] = “Algol” -- String literals are aggregates but no array assignments, so initializer is not an expression (mechanism is less orthogonal)

Aggregates and Qualification Aggregate may be ambiguous: type Vector is array (1.. 3) of Float; procedure Display (V : vector); type Assay is array (1.. 3) of Float; procedure Display (A : assay); … Display ((1.0, 1.2, 1.5)); -- which? ambiguous Display (Vector ‘ (1.0, 1.2, 1.5)); -- OK.

Multidimensional Arrays Aggregates given in row-major order with subaggregates: type Square is array (1.. 3, 1.. 3) of Integer; Unit : constant Square := ( (1, 0,0), (0, 1, 0), (0, 0, 1)); A two-dimensional array is NOT array of arrays: type vector is array (1.. 3) of Integer; type V3 is array (1.. 3) of vector; -- not convertible to Square

Operations on One_Dimensional Arrays Boolean operations extend pointwise: type Set is array (1.. Card) of Boolean; S1, S2, S3 : Set; … S3 := S1 and S2; -- Set Intersection lexicographic comparisons on arrays of discrete types: S1 := (T, T, T); S2 := (T, T, F);.. S2 < S1 -- yields True

Concatenation and Slicing Both operations yield the base type: type Table is array (1..10) of Integer; T1, T2 : Table; … T1 & T2 -- What type? Declaration equivalent to: type Anon is array (integer range <>) of Integer; subtype Table is Anon (1.. 10); T1 & T2, T1 (X.. Y) are of type Anon

A discrete range specifies a slice subtype Sub is Positive range 2.. 4; Label : String (1..10) := “transcends” ; … Label (2.. 4) -- Yields “ran” Label (Integer range 2.. 4) -- Same Label (Sub) -- Ditto

Records type city is record -- Ada Name: String (1..10); Country : String (1..20); Population: integer; Capital : Boolean; end record; struct city { -- C, C++ char* name; char* country; int population bool capital }

Variants Need to treat group of related representations as a single type: type figure_kind is (Circle, Square, Line); type Figure (Kind : Figure_kind) is record Color : color_type; -- defined elsewhere Visible : Boolean; case Kind is when Line => Length : Integer; Orientation: Float; Start : Point; -- defined elsewhere when square => Lower_Left, Upper_Right : Point; when circle => Radius : Integer; Center : Point; end case; end record;

Variants are type safe C1 : Figure (Circle); -- discriminant provides constraint S1 : Figure (Square); … C1. Radius := 15; if S1.Lower_Left = C1.Center then.. function Area (F : Figure) return Float is -- applies to any figure, i.e. subtype begin case F.Kind is when Circle => return Pi * Radius ** 2;..

Discriminant checking C : Figure (Circle); L : Figure (Line); F : Figure; -- illegal, don’t know which kind P1, P2 := Point; … C := (Circle, Red, False, 10, P1); -- record aggregate if C.Orientation then -- illegal, circles have no orientation C := L; -- illegal, different kinds C.Kind := Square; -- Illegal, discriminant is constant Discriminant is visible constant component of object There is a way of specifying a figure that can change kinds

Variants and classes Discriminated types and classes have similar functionalities Discriminated types can be allocated statically Run-time code uses less indirection Compiler can enforce consistent use of discriminants Adding new variants is disruptive –must modify every case statement Variant programming: one procedure at a time Class programming : one class at a time

Free Unions Free unions can be used to bypass the type model: union Value { char* s; // allocated at same address (C semantics) int i; } ; programmer must keep track of current type, e.g. by using an explicit tag: struct Entry { int discr; union { // anonymous component, either s or i. char* s; // if discr = 0 int i; // if discr = 1, but run-time system won’t check };

Discriminated unions and dynamic typing In dynamically-typed languages, only values have types, not names. S = # a floating-point number … S = [1, 2, 3, 4] # a list Run-time values are described by discriminated unions. Discriminant denotes type of value. S = X + Y # arithmetic or concatenation The Variant type in BASIC has the same property. The Tag in a class object functions like a discriminant

Access Types and pointers Related (but distinct) notions: –a value that denotes a memory location –a dynamic name that can designate different objects –a mechanism to separate stack and heap allocation type ptr is access integer; -- Ada: named type typedef ptr int*; -- C, C++ –A value of type (access T) designates a value of type T

Dynamic data structures type Cell; -- an incomplete type type Ptr is access Cell; -- an access to it type Cell is record -- its full declaration value : Integer; next, prev : Ptr; end record; List: Ptr := new Cell ‘(10, null, null); … -- a list is just a pointer to its first element List.next := new Cell ‘(15, null, null); List.next.prev := List;

Incomplete declarations in C++ struct cell { int Value; cell* prev; // legal to mention name cell* next; }; // before end of declaration struct List; // incomplete declaration struct Link { link* succ; List* member_of; }; // a pointer to it struct List { // full definition Link* head: // mutual references };

Pointers and dereferencing Need notation to distinguish pointer from designated object –in Ada : Ptr, Ptr.all –in C : Ptr, Ptr* –in Java: no notion of pointer For pointers to composite values, dereference can be implicit: –in Ada : C1.Value equivalent to C1.all.Value –in C++ : distinguish C1.Value and C1 -> Value –in both : pointers to arrays are indexable: arr_ptr (5), arr_ptr[5]

Three models for arrays In Ada, arrays can be static or dynamic. Arrays are objects with assignment. In C++ arrays can be static only if they have static bounds. There is no array assignment. In Java arrays are always dynamic, assignment is a reference assignment.

Variations on Strings: Ada Strings are arrays: type String is array (positive range <>) of character; type Str_Ptr is access String; Ptr1, Ptr2 : Str_Ptr; -- initially null Title : String := “Brave New World” ; -- fixed size Ptr3 : Str_Ptr := new String’(“Island”); … Ptr1 := Ptr3; -- pointer assignment makes synonyms Ptr1.all := “what??”; -- array assignment: must be same size Ptr1 := new String (“the genius and the goddess”); Title := Ptr1.all; -- run time error: sizes don’t match

Variations on Strings: C++ char* name1, name2; char title[ ] = “brave new world”; // 16 characters: implicit 0 at end char* t = “island”; // pointer to constant array name1 = new char[16]; // allocate dynamic storage const char* ptr = &title[0]; // pointer to local constant array … while (*name1++ = *ptr++); // amusing C idiom name1 [0] = ‘B’; // title not affected t [0] = “I”; // illegal: string literal is constant semantic equivalence: a[k] = * (a + k)

Variations on Strings: Java Strings are classes, not arrays: need special notation for indexing and slicing. String values are constants: need to use arrays of characters to modify strings. String name = “Eyeless in Gaza”; … name = name + “(“ “); // assign different value // implicit conversion to string: “Eyeless in Gaza (1939)” if (name.StringAt (0) == ‘E’ ) { // true

Pointers and safety Pointers create aliases: accessing the value through one name affects the retrieval through the other: int* tab1, tab2; … tab1 = new int [10]; // allocate tab2 = tab1; // share delete (tab1); // discard storage tab2 [5] =.. // error, tab2 does not denote anything

Dangling references If we can point to local storage, we can create a reference to an undefined value: int* f ( ) { // returns a pointer to an integer int local; // variable on stack frame of f … return local&; // reference to local entity }; int x = f ( ); … x // stack may have been overwritten

Deallocation Manual deallocation is potentially dangerous, because not all current references to an object may be visible. System is safer if storage reclamation is automated. Manual solution: make deallocation more explicit: procedure free is new Ada.Unchecked_Deallocation (String, Ptr); Semi-automatic solution (Ada, C++): destructors, controlled types Automatic Solution (Java, ML): garbage collector