Examples
The following C function allocates a variable-length array of a specified size, fills it with floating-point values, then passes it to another function for processing. Because the array is declared as an automatic variable, its lifetime ends when the read_and_process
function returns.
Following is the same example in Ada. Note that Ada arrays carry their bounds with them, there is no need to pass the length to the Process function.
type Vals_Type is array (Positive range <>) of Float; function Read_And_Process (N : Integer) return Float is Vals : Vals_Type (1 .. N); begin for I in 1 .. N loop Vals (I) := Read_Val; end loop; return Process (Vals); end Read_And_Process;The equivalent Fortran 90 function is:
function read_and_process(n) result(o) integer,intent(in)::n real::o real,dimension(n)::vals real::read_val, process integer::i do i = 1,n vals(i) = read_val end do o = process(vals, n) end function read_and_processThe following COBOL fragment declares a variable-length array of records, DEPT-PERSON
, having a length (number of members) specified by the value of PEOPLE-CNT
.
The following C# fragment declares a variable-length array of integers. The "unsafe" keyword would require an assembly containing this code to be marked as unsafe.
unsafe void declareStackBasedArray(int size) { int *pArray = stackalloc int; pArray = 123; }Read more about this topic: Variable-length Array
Famous quotes containing the word examples:
“There are many examples of women that have excelled in learning, and even in war, but this is no reason we should bring em all up to Latin and Greek or else military discipline, instead of needle-work and housewifry.”
—Bernard Mandeville (16701733)
“No rules exist, and examples are simply life-savers answering the appeals of rules making vain attempts to exist.”
—André Breton (18961966)
“Histories are more full of examples of the fidelity of dogs than of friends.”
—Alexander Pope (16881744)