Docsity

Lecture 42: Modern Programming - Records, Pointers, Unions, and Arithmetic, Exercises of Programming Languages

Toggle sidebar
bg1
Modern Programming Languages
Lecture # 42
Records
Design Issues:
1. What is the form of references?
2. What unit operations are defined?
Record Definition Syntax
- COBOL uses level numbers to show nested records; others use recursive
definitions
Record Field References
1. COBOL
field_name OF record_name_1 OF ... OF
record_name_n
2. Others (dot notation)
record_name_1.record_name_2. ...
.record_name_n.field_name
- Fully qualified references must include all record names
- Elliptical references allow leaving out record names as long as the reference is
unambiguous
- Pascal and Modula-2 provide a with clause to abbreviate references
Record Operations
docsity.com
pf3
pf4

Partial preview of the text

Download Lecture 42: Modern Programming - Records, Pointers, Unions, and Arithmetic and more Exercises Programming Languages in PDF only on Docsity!

Modern Programming Languages

Lecture # 42

Records

Design Issues:

  1. What is the form of references?
  2. What unit operations are defined?

Record Definition Syntax

  • COBOL uses level numbers to show nested records; others use recursive definitions

Record Field References

1. COBOL

field_name OF record_name_1 OF ... OF record_name_n

  1. Others (dot notation) record_name_1.record_name_2. ... .record_name_n.field_name
  • Fully qualified references must include all record names
  • Elliptical references allow leaving out record names as long as the reference is unambiguous
  • Pascal and Modula-2 provide a with clause to abbreviate references

Record Operations

  1. Assignment
    • Pascal, Ada, and C allow it if the types are identical
    • In Ada, the RHS can be an aggregate constant
  2. Initialization
    • Allowed in Ada, using an aggregate constant
  3. Comparison
    • In Ada, = and /=; one operand can be an aggregate constant

Comparing records and arrays

  1. Access to array elements is much slower than access to record fields, because subscripts are dynamic (field names are static)
  2. Dynamic subscripts could be used with record field access, but it would disallow type checking and it would be much slower

Pointers

A pointer type is a type in which the range of values consists of memory addresses and a special value, nil (or null)

Uses:

  1. Addressing flexibility
  2. Dynamic storage management

Fundamental Pointer Operations:

  1. Assignment of an address to a pointer
    1. References (explicit versus implicit dereferencing)

Design Issues

  1. What is the scope and lifetime of pointer variables?
  2. What is the lifetime of heap-dynamic variables?
  3. Are pointers restricted to pointing at a particular type?
  4. Are pointers used for dynamic storage management, indirect addressing, or both?
  5. Should a language support pointer types, reference types, or both?
  6. Pointer arithmetic?

Evaluation of Pointers

  1. Dangling pointers and dangling objects are

x : real; blurb.tagg := true; { it is an integer } blurb.blint := 47; { ok } blurb.tagg := false; { it is a real } x := blurb.blreal; { assigns an integer to a real }

b. The tag is optional!

  1. Ada
    • Discriminated unions are safer than Pascal & Modula-
    • Reasons a. Tag must be present

b. It is impossible for the user to create an inconsistent union (because tag cannot be assigned by itself)

  • All assignments to the union must include the tag value)
  1. C and C++ have free unions (no tags)
  • Not part of their records
  • No type checking of references
  1. Java has neither records nor unions
  • Unions are potentially unsafe in most languages (except Ada)

Arithmetic Expressions:

Arithmetic expressions consist of operators, operands, parentheses, and function calls

Design issues for arithmetic expressions:

  1. What are the operator precedence rules?
  2. What are the operator associativity rules?
  3. What is the order of operand evaluation?
  4. Are there restrictions on operand evaluation side effects?
  5. Does the language allow user-defined operator overloading?
  6. What mode mixing is allowed in expressions?
  7. Conditional expressions?