Anonymous user
Arrays: Difference between revisions
m
→{{header|Modula-3}}
No edit summary |
|||
Line 5,032:
=={{header|Modula-3}}==
<lang modula3>VAR
Defines
<lang modula3>VAR
VAR
Open (dynamic) arrays can be be defined by creating a reference to an array of an unspecified size:
<lang modula3>
VAR
Defines an open array of a currently unknown size.
Open arrays must first be initialized via a call to the built-in function NEW, passing in the type and the size of the array.
arr[1] := 10;</lang>▼
The allocation is performed on the heap and all elements are initialized to 0:
<lang modula3>openArray := NEW(TOpenIntArray, 10);</lang>
Initializes the open array to hold 10 elements, indexed 0 through 9. Modula-3 uses garbage collection for heap allocated data by default, so once all references to the open array go out of scope, the memory it occupied is de-allocated automatically.
Retrieval or insertion of elements and determining array bounds is performed using the same built-in functions regardless of the array kind. Though open arrays must first be de-referenced when passing them to such functions. Assuming we have made the declarations above, we can do the following:
<lang modula3> VAR
staticArraySize := NUMBER(staticArray);
staticArrayElement := staticArray[4];
openArraySize := NUMBER(openArray^); (* Note the dereference. *)
openArrayElement := openArray[9];</lang>
<lang modula3>staticArray[4] := 100;
=={{header|Monte}}==
|