Concepts of Programming Languages

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Advertisements

1 Chapter 6 Data Types What is a data type? A set of values versus A set of values + set of operations on those values.
Kernighan/Ritchie: Kelley/Pohl:
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 6 Data Types: Structured types.
CS 330 Programming Languages 11 / 08 / 2007 Instructor: Michael Eckmann.
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.
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
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.
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.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
ISBN 0-321— Chapter 6 Structured Data Types Arrays Associated Arrays Records Unions.
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 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.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
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.
1 Data Types Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types Union Types Pointer 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.
Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.
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.
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.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
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
Data Types In Text: Chapter 6.
Stack and Heap Memory Stack resident variables include:
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 CSCE 343.
Chapter 6: Data Types Saad Alshahrani
CSC 533: Programming Languages Spring 2015
Chapter 6 Data Types.
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
Concepts of programming languages
Concepts of programming languages Names, Bindings, and Scopes
Data Types In Text: Chapter 6.
Arrays, For loop While loop Do while loop
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.
Dynamic Memory Allocation
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Chapter 6 Data Types.
Presentation transcript:

Concepts of Programming Languages Dr. Mohamed Yehia Dahab

Array Types An array is an aggregate of homogeneous data elements in which an individual element is identified by its position in the aggregate, relative to the first element

Array Design Issues What types are legal for subscripts? Are subscripting expressions in element references range checked? When are subscript ranges bound? When does allocation take place? What is the maximum number of subscripts? Can array objects be initialized? Are any kind of slices allowed?

Array Indexing Indexing (or subscripting) is a mapping from indices to elements array_name (index_value_list)  an element Index Syntax FORTRAN, PL/I, Ada and matlab use parentheses Ada explicitly uses parentheses to show uniformity between array references and function calls because both are mappings Most other languages use brackets

Arrays Index (Subscript) Types FORTRAN, C and Java: integer only Ada and Pascal: any ordinal type (https://ideone.com/JPfzA9) program ideone; type Color = (Red, Orange, Black, White, Yellow, Green, Blue); var A1 : array [-10 .. 10] of real; A2 : array['A'..'Z'] of integer; A3 : array[False .. True] of Color ; A4 : array[Red .. Yellow] of integer;  begin A1[-5] := 3.1; A2['C'] :=17; A3 [True] := Red; A4 [Yellow] := 55; end.

Arrays Index (Subscript) Types (Cont’) C, C++, Perl, and Fortran do not specify range checking (https://ideone.com/pE7XX3) #include <stdio.h>   int main(void) { int Array[5]; Array[10] = 55; printf("\n%d\n",Array[10]); return 0; }

Arrays Index (Subscript) Types (Cont’) Java, ML, matlab, and C# specify range checking % Matlab Code A=[55 7 8 9]; display (A(1)) % display 55 display (A(0)) % index must be a positive integer or logical display (A(4)) % display 9 display (A(5)) % Attempted to access A(5); index out of bounds because numel(A)=4 display (A(3>1)) % display 55 display (A(1>3)) % display []

Subscript Binding and Array Categories Static: subscript ranges are statically bound and storage allocation is static (before run-time) Advantage: efficiency (no dynamic allocation) Fixed stack-dynamic: subscript ranges are statically bound, but the allocation is done at declaration time Advantage: space efficiency

Subscript Binding and Array Categories (Cont’) Stack-dynamic: subscript ranges are dynamically bound and the storage allocation is dynamic (done at run-time) Advantage: flexibility (the size of an array need not be known until the array is to be used) Fixed heap-dynamic: similar to fixed stack- dynamic: storage binding is dynamic but fixed after allocation (i.e., binding is done when requested and storage is allocated from heap, not stack)

Subscript Binding and Array Categories (continued) Heap-dynamic: binding of subscript ranges and storage allocation is dynamic and can change any number of times Advantage: flexibility (arrays can grow or shrink during program execution)

Subscript Binding and Array Categories (Cont’) C and C++ arrays that include static modifier are static C and C++ arrays without static modifier are fixed stack-dynamic int Arr[20]; Ada arrays can be stack-dynamic C and C++ provide fixed heap-dynamic arrays (using malloc/free or new/delete) Perl and JavaScript support heap-dynamic arrays

C Language (Fixed Heap-dynamic) Using malloc() function Allocates requested size of bytes and returns a pointer first byte of allocated space <pointer variable>=(<cast-type>*)malloc(<byte-size>); Using free() function to dellocate the previously allocated space

Cuda C/C++ using cudaMalloc() (Fixed Heap-dynamic) A void **  is just a pointer to a pointer to memory with an unspecified type Host and device memory are separate entities Device pointers point to GPU memory Host pointers point to CPU memory int size = 100 * sizeof (int); int *d_a; cudaMalloc((void **)&d_a, size); … cudaFree(d_a);

C Language (Fixed Heap-dynamic) (Cont’) #include <stdio.h> #include <stdlib.h> int main(){ int *ptr; ptr=(int*)malloc(10*sizeof(int)); //memory allocated using malloc if(ptr==NULL) { printf("Error!"); exit(0); } //using ptr free(ptr); return 0; https://ideone.com/7ojfAs

Java ArrayList (Heap-dynamic) import java.util.*; import java.io.*; class TestArrayList { public static void main (String[] args){ ArrayList A = new ArrayList(); A.add(30); A.add(130); A.add("3dfdf"); System.out.println(A.size()); System.out.println(A.get(1)); //zero based System.out.println(A.get(2)); } https://ideone.com/y4ooYK Heterogeneous Arrays

Java ArrayList (Heap-dynamic) (Cont’) import java.util.*; import java.io.*; class TestGenericArrayList { public static void main (String[] args){ ArrayList <Integer> intList = new ArrayList<Integer>(); ArrayList <String> stringList = new ArrayList<String>(); intList.add(30); intList.add(10); stringList.add("hggfhfg"); stringList.add("dgdfgdfgrtre"); System.out.println(intList.size()); System.out.println(stringList.get(1)); } https://ideone.com/DuWSNK

Array in Python A better analogy would be to the ArrayList class Built-in functions and methods: append() remove() len() count(object) + for concatenation … NumPy is an open-source add-on modules to Python that provide common mathematical and numerical routines in pre-compiled, fast functions

Array in Python (Cont’) A.append(12) A.insert(0,13) print (A) A[1] = 10 A = A * 3 R = range(4) A = A + R print (13 in A) A.sort() print (A[2:5]) A.append("erere") [13, 12] [13, 10, 13, 10, 13, 10] [13, 10, 13, 10, 13, 10, 0, 1, 2, 3] True [0, 1, 2, 3, 10, 10, 10, 13, 13, 13] [2, 3, 10] https://ideone.com/zJD4xl Heterogeneous Arrays https://ideone.com/zJD4xl

Array Initialization Some language allow initialization at the time of storage allocation C, C++, Java, C# example int list [] = {4, 5, 7, 83} Character strings in C and C++ char name [] = “freddie”; Arrays of strings in C and C++ char *names [] = {“Bob”, “Jake”, “Joe”]; Java initialization of String objects String[] names = {“Bob”, “Jake”, “Joe”};

Arrays Operations APL provides the most powerful array processing operations for vectors and matrixes as well as unary operators (for example, to reverse column elements) Ada allows array assignment but also catenation Fortran provides elemental operations because they are between pairs of array elements For example, + operator between two arrays results in an array of the sums of the element pairs of the two arrays

Rectangular and Jagged Arrays A rectangular array is a multi-dimensioned array in which all of the rows have the same number of elements and all columns have the same number of elements A jagged matrix has rows with varying number of elements Possible when multi-dimensioned arrays actually appear as arrays of arrays

Java is using Jagged arrays Java Example int array[][] = new int[3][]; array[0] = new int[3]; array[1] = new int[2]; array[2] = new int[5]; Java is using Jagged arrays What about the following programming language: Matlab Pascal C python

Slices A slice is some substructure of an array; nothing more than a referencing mechanism Slices are only useful in languages that have array operations

Slice Examples Fortran 95 Python Integer, Dimension (10) :: Vector Integer, Dimension (3, 3) :: Mat Integer, Dimension (3, 3, 4) :: Cube Vector (3:6) is a four element array Python A[2:5] is a three element array

Slices Examples in Fortran 95