| | 1 | = Arrays = |
| | 2 | |
| | 3 | Thoughts on arrays and related issues. |
| | 4 | |
| | 5 | * drop C's silly pointer-array pun. Pointers and arrays have nothing to do with each other. In C, |
| | 6 | {{{ |
| | 7 | void f(double[] x) {…} |
| | 8 | double a[10]; |
| | 9 | … |
| | 10 | f(a); |
| | 11 | … |
| | 12 | }}} |
| | 13 | is automatically converted to |
| | 14 | {{{ |
| | 15 | void f(double *x) {…} |
| | 16 | double a[10]; |
| | 17 | … |
| | 18 | f(&a[0]); |
| | 19 | … |
| | 20 | }}} |
| | 21 | In CIVL-C (CIVL?), you write the one you mean. |
| | 22 | |
| | 23 | * 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. |
| | 24 | |
| | 25 | * 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: |
| | 26 | {{{ |
| | 27 | $message[] append_message($message[] queue, $message m); |
| | 28 | }}} |
| | 29 | 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. |
| | 30 | |
| | 31 | * 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. |