Changes between Version 99 and Version 100 of IR2
- Timestamp:
- 05/14/21 10:41:03 (5 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
IR2
v99 v100 17 17 18 18 1. **Do variables have default initial values?** 19 * No. Every variable must be explicitly initialized. However, `$undef` can be used as the initializer, indicating that the variable is undefined. Even in this case, the variable has some unspecified value of its type. Details are below.19 * No. Every variable (other than a variable designating a system or abstract function) must must be explicitly initialized. However, `$undef` can be used as the initializer, indicating that the variable is undefined. Even in this case, the variable has some unspecified value of its type. Details are below. 20 20 1. **How do you initialize a variable?** 21 * In the declaration. For example ` n=$new($int);` will assign `n` an arbitrary integer, while `n=0;` will assign the integer `0` to `n`.21 * In the declaration. For example `$int n=$new($int);` will assign `n` an arbitrary integer, while `$int n=0;` will assign the integer `0` to `n`. 22 22 1. **How is an array allocated?** 23 * An array variable `a` is declared with a decl such as `T a[] = $new_array(n, T);` which assigns to `a` a new array value for an array of length `n` of elements of type `T` in which all entries are undefined. (Alternatively, `T a[] = $new(T[n])` will create an array of arbitrary defined values.) For heap-allocation, `T * p = $undef;` followed by the statement `p = $alloc (&heap, n, T);` will add a new object to the heap and return a pointer to the first element. An `$alloc`-ed object can be deallocated with `$free(p);`.23 * An array variable `a` is declared with a decl such as `T a[] = $new_array(n, T);` which assigns to `a` a new array value for an array of length `n` of elements of type `T` in which all entries are undefined. (Alternatively, `T a[] = $new(T[n])` will create an array of arbitrary defined values.) For heap-allocation, `T * p = $undef;` followed by the statement `p = $alloc<T>(&heap, n);` will add a new object to the heap and return a pointer to the first element. An `$alloc`-ed object can be deallocated with `$free(p);`. 24 24 1. **Is there an "array-pointer pun", as in C?** 25 25 * No, if you want a pointer to element 0 of an array, you have to explicitly write something like `&a[0]`. … … 155 155 * Arrays are similar to sequences. The main differences are as follows: 156 156 * An object (i.e., a variable or component of an object) of array type is initialized once, then will never be assigned to again. Hence there cannot be a statement of the form `a=...` where `a` has array type. 157 * After initialization, an object o rarray type can appear only as the first (left) argument to the subscript operator `[]` or as the argument to the `$length` operator.157 * After initialization, an object of array type can appear only as the first (left) argument to the subscript operator `[]` or as the argument to the `$length` operator. 158 158 * Arrays are mutable. As in C, the left hand size of an assignment may have the form `a[i]`, where `a` is an object of array type. Sequences are immutable. 159 159 * Elements of an array are addressable, i.e., one can form a pointer such as `&a[i]`. This is not possible with sequences, sets, maps, or relations---there is no way to have a pointer to any component of such a type.
