| 23 | | * An array variable `a` is declared with a decl such as `T a[];`, and then a statement such as `a=$new(T[n]);` will assign to `a` a new arbitrary array value for an array of length `n` of elements of type `T`. For heap-allocation, a pointer is declared with a decl such as `T * p;`, and a heap variable is also declared somewhere with a decl such as `$heap heap;` and then a statement such as `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[];`, and then a statement such as `a=$new_array(n, T);` will assign to `a` a new array value for an array of length `n` of elements of type `T` in which all entries are undefined. (Alternatively, `a=$new(T[n])` will create an array of arbitrary defined values.) For heap-allocation, a pointer is declared with a decl such as `T * p;`, and a heap variable is also declared somewhere with a decl such as `$heap heap;` and then a statement such as `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);`. |
| 244 | | * need to think about `$defined`. How can you allocate an array and have all cells undefined? |
| | 248 | * explanation of `$defined`: a bit is associated to each memory location. Memory locations are created by declaring variables and allocation. Every variable initially holds an undefined value. Memory locations become defined by assigning to them defined values. |
| | 249 | * Constants are defined values. This includes integer and real constants, and `NULL`. |
| | 250 | * An operation returns a defined value if all inputs are defined and the inputs are acceptable for the specific operation. |
| | 251 | * It is arguable about whether we should make division by 0, out of bound array indexes, etc., undefined values. They could be defined. |
| | 252 | * Once an array is allocated with `$new_array`, the array itself is still undefined and all elements are undefined. However `$defined($length(a))` will return `$true`. |
| | 253 | * Once an object is heap allocated with `p=$alloc(...)`, `p` becomes defined, but `*p`, `*(p+1)`, ..., are all undefined as they hold uninitialized data. |
| | 254 | * A pointer that points to the location one past the last element of an array or object is defined, but not valid. The same holds for `NULL`. |
| | 255 | * If a pointer points to an object on the heap and that object is freed, the pointer becomes undefined. Similarly, if it points into an object that goes out of scope, the pointer becomes undefined. |
| | 256 | * If an object is heap allocated then a pointer `p` into that object will be defined, but `*p` will be undefined (as the memory location pointed to by `p` has uninitialized data), but `$valid(p)` will return `$true`, indicating it is safe to write to that location. |