wiki:Arrays

Version 1 (modified by siegel, 13 years ago) ( diff )

--

Arrays

Thoughts on arrays and related issues.

  • drop C's silly pointer-array pun. Pointers and arrays have nothing to do with each other. In C,
    void f(double[] x) {…}
    double a[10];
    …
      f(a);
    …
    

is automatically converted to

void f(double *x) {…}
double a[10];
…
  f(&a[0]);
…

In CIVL-C (CIVL?), you write the one you mean.

  • Arrays are values just like any other kind of value (similar to structs). They can be passed as arguments to functions and returned by functions.
  • The incomplete array type "array of t", denoted t[], can be used (almost) anywhere a type is expected, including the element type of an array type. The domain of t[] is all arrays of t, of any length. A complete type t[n] is a subtype of t[]. Example:
      $message[] append_message($message[] queue, $message m);
    

takes an array of messages and returns an array one longer which is equivalent to the original array with $m$ added. There is no "sharing" between these two arrays.

  • Instead of malloc, introduce an operator $alloc(heap, type) where heap is an expression of type pointer-to-heap and type is a type name. The type of this expression is pointer-to-type. It allocates an object of the given type on the specified heap and returns a pointer to that object.
Note: See TracWiki for help on using the wiki.