Docsity

Programming Languages: Arrays, Pointers, Lists, and Files - A Roadmap, Study notes of Programming Languages

Partial preview of the text

Download Programming Languages: Arrays, Pointers, Lists, and Files - A Roadmap and more Study notes Programming Languages in PDF only on Docsity!

1

Programming Languages

Tevfik Koşar

Lecture - XVIII March 23rd^ , 2006

2

Roadmap

  • Arrays
  • Pointers
  • Lists
  • Files and I/O

3

Arrays

  • Two layout strategies for arrays
    • Contiguous elements
    • Row pointers
  • Row pointers
    • an option in C
    • allows rows to be put anywhere - nice for big arrays on machines with segmentation problems
    • avoids multiplication
    • nice for matrices whose rows are of different lengths
      • e.g. an array of strings
    • requires extra space for the pointers

4

Arrays

7

Arrays

  • Example (continued)

We could compute all that at run time, but we can make do with fewer subtractions:

== (i * S1) + (j * S2) + (k * S3)

  • address of A
  • [(L1 * S1) + (L2 * S2) + (L3 * S3)] The stuff in square brackets is compile-time constant that depends only on the type of A

8

Strings

  • Strings are really just arrays of characters
  • They are often special-cased, to give them

flexibility (like polymorphism

or dynamic sizing) that is not available for

arrays in general

  • It's easier to provide these things for strings than for arrays in general because strings are one- dimensional and (more important) non-circular

9

Sets

  • We learned about a lot of possible

implementations

  • Bitsets are what usually get built into programming languages
  • Things like intersection, union, membership, etc. can be implemented efficiently with bitwise logical instructions
  • Some languages place limits on the sizes of sets to make it easier for the implementor - There is really no excuse for this

10

Pointers And Recursive Types

  • Pointers serve two purposes:
    • efficient (and sometimes intuitive) access to elaborated objects (as in C)
    • dynamic creation of linked data structures, in conjunction with a heap storage manager
  • Several languages (e.g. Pascal) restrict pointers

to accessing things in the heap

  • Pointers are used with a value model of

variables

  • They aren't needed with a reference model

13

Pointers And Recursive Types

  • C pointers and arrays

int *a == int a[] int **a == int *a[]

  • BUT equivalences don't always hold
    • Specifically, a declaration allocates an array if it specifies a size for the first dimension
    • otherwise it allocates a pointer int **a, int *a[] pointer to pointer to int int *a[n], n-element array of row pointers int a[n][m], 2-d array

14

Pointers And Recursive Types

  • Compiler has to be able to tell the size of the

things to which you point

  • So the following aren't valid: int a[][] bad int (*a)[] bad
  • C declaration rule: read right as far as you can (subject to parentheses), then left, then out a level and repeat int a[n], n-element array of pointers to integer int (a)[n], pointer to n-element array of integers

15

Pointers And Recursive Types

  • Problems with dangling pointers are due to
    • explicit deallocation of heap objects
      • only in languages that have explicit deallocation
    • implicit deallocation of elaborated objects
  • Two implementation mechanisms to catch dangling pointers - Tombstones - Locks and Keys

16

Pointers And Recursive Types

19

Pointers And Recursive Types

  • Garbage collection with reference counts

20

Pointers And Recursive Types

  • Mark-and-sweep
    • commonplace in Lisp dialects
    • complicated in languages with rich type structure, but possible if language is strongly typed
    • achieved successfully in Cedar, Ada, Java, Modula-3, ML
    • complete solution impossible in languages that are not strongly typed
    • conservative approximation possible in almost any language (Xerox Portable Common Runtime approach)

21

Pointers And Recursive Types

  • Garbage

collection

with pointer

reversal

22

Lists

  • A list is defined recursively as either the

empty list or a pair consisting of an object

(which may be either a list or an atom) and

another (shorter) list

  • Lists are ideally suited to programming in functional and logic languages - In Lisp, in fact, a program is a list, and can extend itself at run time by constructing a list and executing it
  • Lists can also be used in imperative programs