Dynamic Memory Allocation and Strong Typing in Programming Languages, Exercises of Programming Languages

Dynamic memory allocation through explicit directives and implicit assignments in programming languages. It also explores the concept of strong typing and its impact on type compatibility and error detection. Various programming languages, such as c++, java, and fortran, are examined in relation to dynamic memory allocation and strong typing.

Typology: Exercises

2011/2012

Uploaded on 08/04/2012

dhanvin
dhanvin 🇮🇳

4.2

(14)

108 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Allocated and de-allocated by explicit directives, specified by the programmer,
which take effect during execution
Referenced only through pointers or references
Examples
Dynamic objects in C++ (via new and delete)
All objects in Java
Advantage
Provide dynamic storage management
Disadvantage
Inefficient and unreliable
Implicit Heap Dynamic Variables
Allocation and de-allocation is caused by assignment statements
Example
All variables in Snobol
Advantage
Flexibility
Disadvantages
Inefficient, because all attributes are dynamic
Loss of error detection
Modern Programming Languages
Lecture # 41
Type Checking
Generalizes the concept of operands and operators to include subprograms
and assignments
Type checking is the activity of ensuring that the operands of an operator are
of compatible types
A compatible type is one that is either legal for the operator, or is allowed
under language rules to be implicitly converted, by compiler generated code, to
a legal type. This automatic conversion is called coercion
A type error is the application of an operator to an operand of an inappropriate
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Dynamic Memory Allocation and Strong Typing in Programming Languages and more Exercises Programming Languages in PDF only on Docsity!

  • Allocated and de-allocated by explicit directives, specified by the programmer, which take effect during execution
  • Referenced only through pointers or references
  • Examples
    • Dynamic objects in C++ (via new and delete)
    • All objects in Java
  • Advantage
    • Provide dynamic storage management
  • Disadvantage
    • Inefficient and unreliable

Implicit Heap Dynamic Variables

  • Allocation and de-allocation is caused by assignment statements
  • Example
    • All variables in Snobol
  • Advantage
    • Flexibility
  • Disadvantages
    • Inefficient, because all attributes are dynamic
    • Loss of error detection

Modern Programming Languages Lecture # 41

Type Checking

  • Generalizes the concept of operands and operators to include subprograms and assignments
  • Type checking is the activity of ensuring that the operands of an operator are of compatible types
  • A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted, by compiler generated code, to a legal type. This automatic conversion is called coercion
  • A type error is the application of an operator to an operand of an inappropriate

type

  • If all type bindings are static, nearly all type checking can be static
  • If type bindings are dynamic, type checking must be dynamic
  • A programming language is strongly typed if type errors are always detected

Strongly Typed Languages?

  • FORTRAN 77 is not strongly typed
    • Parameters, EQUIVALENCE
  • C and C++ are not
    • Parameter type checking can be avoided
    • Unions are not type checked
  • Pascal is not
    • Variant records
  • Modula-2 is not
    • Variant records
  • Ada is, almost
    • UNCHECKED CONVERSION is a loophole
    • Coercion rules strongly affect strong typing; they can weaken it considerably (C++ versus Ada)
  • Advantage of strong typing
    • Allows the detection of the misuse of variables that result in type errors

Type Compatibility

• Type compatibility by name means that the two variables have compatible

types if they are in either the same declaration or in declarations that use the same type name

• Easy to implement but highly restrictive

  • Sub ranges of integer types are not compatible with integer types

Data Types

• Design Issues

  • What is the syntax of references to variables?
  • What operations are defined and how are they specified?

Primitive Data Types

• Not defined in terms of other data types

• Integer

  • Almost always an exact reflection of the hardware, so the mapping is trivial

• There may be as many as eight different integer types in a language

Floating Point

  • Models real numbers, but only as approximations
  • Languages for scientific use support at least two floating-point types; sometimes more
  • Usually exactly like the hardware, but not always; some languages allow accuracy specs in code e.g. (Ada)

values, which are symbolic constants

  • Design Issue
    • Should a symbolic constant be allowed to be in more than one type definition?

Examples

  • Pascal
    • Cannot reuse constants
    • They can be used for array subscripts e.g. for variables, case selectors
    • NO input or output
    • Can be compared
  • Ada
    • Constants can be reused (overloaded literals)
    • Disambiguates with context or type_name
    • CAN be input and output
  • C and C++
    • Like Pascal, except they can be input and output as integers
  • Java does not include an enumeration types
  • C# includes them

Evaluation (of enumeration types)

  • Useful for readability
    • e.g. no need to code a color as a number
  • Useful for reliability
    • e.g. compiler can check operations and ranges of values

Subrange Type

  • An ordered contiguous subsequence of an ordinal type
  • Pascal
    • Subrange types behave as their parent types; can be used as for variables and array indices
    • E.g. type pos = 0 .. MAXINT;
  • Ada
    • Subtypes are not new types, they are just constrained existing types (so they are compatible)
    • Can be used as in Pascal, plus case constants
    • E.g.

subtype POS_TYPE is

INTEGER range 0 ..INTEGER'LAST;

Evaluation (of enumeration types)

  • Aid to readability
  • Reliability - restricted ranges add error detection

Implementation of user-defined ordinal types

  • Enumeration types are implemented as integers
  • Subrange types are the parent types with code inserted (by the compiler) to restrict assignments to subrange variables

Arrays

  • 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
  • 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?

Subscript Types

  • FORTRAN, C, C++
    • int only
  • Pascal
    • Any ordinal type (int, boolean, char, enum)
  • Ada
    • int or enum (includes boolean and char)
  • Java
    • integer types only

Four Categories of Arrays(based on subscript binding and binding to storage)

  • Static - range of subscripts and storage bindings are static e.g. FORTRAN 77, some arrays in Ada
  • Advantage : execution efficiency (no allocation or de-allocation)
  • Fixed stack dynamic - range of subscripts is statically bound, but storage is bound at elaboration time e.g. C local arrays are not static
  • Advantage : space efficiency
  • Stack-dynamic - range and storage are dynamic, but fixed from then on for the variable’s lifetime e.g. Ada declare blocks declare STUFF : array (1..N) of FLOAT; begin ... end;
  • Advantage : flexibility - size need not be known until the array is about to be used