Docsity

Miscellaneous Data Types - Programming Languages | CS 403, Study notes of Programming Languages

Partial preview of the text

Download Miscellaneous Data Types - Programming Languages | CS 403 and more Study notes Programming Languages in PDF only on Docsity!

Lecture 23 – Thursday, March 27 • Today

  • Miscellaneous Data Types– Exam review
    • Reading Assignments For Next Class
      • pp. 68-69, 191-3, 248-
        • Assignments
          • None
            • Exams:
              • Exam 2: Thursday, April 3

Data Types • Type equivalence, compatibility, coercion

(already discussed)

  • Implementation of various types:
    • Scalars (ints and floats)– Records– Arrays– Strings
      • Assignment and equality testing

Array Issues •^

Typically stored contiguously

-^

Two-dimensional arrays:– Row major order: Rows stored contiguously– Column major order: Columns stored contiguously

-^

Row-major order more common– Allows two-dimensional arrays to be treated as “arrays of

arrays” (where each sub-array is stored together)

  • In C++: int a[10][20]; a[3,5] and a[3][5] are the same (and

a[x] is stored contiguously)

Array Sizing Issues •^

Three basic models:– int a[10]: Size of array/addressing determined at compile-

time (“relatively static”)

  • cin >> x; int a[x]: Size of array/addressing determined at

run-time. Once determined, it cannot be changed. (“semi-dynamic”)

  • int a[10]: Size of array can increase and decrease

dynamically at run-time. Need operations to change size(a.add(), a.delete(), etc.) (“dynamic”)

•^

Latter two models not available in C++

-^

Third model is available as a vector class

Strings •^

Strings could be implemented as arrays of characters.

-^

Problem: It is inconvenient to view them/treat them asstatically declared and allocated arrays.– String s;– s = “cat”;– s = “Now is the time for all good men”– How big is s? (could declare String s[50], but then you have to deal

with wasted space or arbitrary limits)

-^

Most languages simply treat them as fully dynamic arrays thatcan grow and shrink automatically– Space will typically be allocated from the heap

Assignment and Equality Testing •^

Suppose strings are implemented as pointers tosome string of characters in the heap.

-^

Two possible implementations of assignment (a = b):– Deep copy: Make a copy of the string referred to by b,

make a refer to the copy.

  • Shallow copy: Copy the pointer in b to a. -^

Shallow copy: Cheap, but results in aliasing (andpotential resulting problems)

-^

Deep copy: Expensive, but safer

A Class Example • Stack:

  • Implemented with an array and a top pointer
    • Two possible object values:
  • These are bit-wise unequal, but should be

viewed as equal (Why?)

Exam Review (revised slightly after class) •^

Static vs. dynamic scope

-^

Separate compilation

-^

Namespaces and packages

-^

Static and dynamic linking

-^

Variable initialization

-^

Expression evaluation

-^

Conditional and iteration structures

-^

Parameter passing

-^

Data types:– Coercion/compatibility– Basic type implementation