Unlike other languages, there seems no allocate
or new
syntax in Chapel for allocating arrays on the heap, but rather use the usual "declaration"-like syntax. For example, in the following code, I "declare" two arrays A
and B
in a function based on formal (dummy) arguments:
proc test( n, D )
{
var A: [1..n] real; // local array
var B: [D] real; // local array
writeln( "A.domain = ", A.domain );
writeln( "B.domain = ", B.domain );
}
test( 3, {2..5} ); // request small arrays
test( 10**7, {-10**7..10**7} ); // request large arrays
This gives the following result:
A.domain = {1..3}
B.domain = {2..5}
A.domain = {1..10000000}
B.domain = {-10000000..10000000}
Because no stack overflow occurs (despite the large size of B
), is it OK to assume that the above syntax always allocates A
and B
on the heap regardless of their size?
Also, assignment (or re-assignment) of a domain variable seems to play the role of allocation (or re-allocation) of an array. For example, the following code works as expected. In this case, does the allocation always occur on the heap (again)?
var domC: domain(1);
var C: [domC] real;
writeln( "C = ", C );
domC = { 1..3 }; // assign a domain
C = 7.0; // assign some value to the array
writeln( "C = ", C );
domC = { -1..5 }; // re-assign a domain
writeln( "C = ", C );
Result:
C =
C = 7.0 7.0 7.0
C = 0.0 0.0 7.0 7.0 7.0 0.0 0.0
Finally, is it not necessary for the user to deallocate
or delete
these arrays manually, but rather the system automatically deallocates them as needed?