Skip to content

Commit

Permalink
Clarifies when allocate was introduced closes #2132
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ committed Jun 29, 2018
1 parent db0f520 commit bfd3f57
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions doc/Language/nativecall.pod6
Expand Up @@ -270,12 +270,22 @@ my $ints = CArray[int32].allocate($number_of_ints); # instantiates an array with
my $n = get_n_ints($ints, $number_of_ints);
=end code
I<Note>: C<allocate> was introduced in Rakudo 2018.05. Before that, you had to
use this mechanism to extend an array to a number of elements:
=begin code :skip-test
my $ints = CArray[int32].new;
my $number_of_ints = 10;
$ints[$number_of_ints - 1] = 0; # extend the array to 10 items
=end code
The memory management of arrays is important to understand. When you create an
array yourself, then you can add elements to it as you wish and it will be
expanded for you as required. However, this may result in it being moved in
memory (assignments to existing elements will never cause this, however). This
means you'd best know what you're doing if you twiddle with an array after passing
it to a C library.
means you'd best know what you're doing if you twiddle with an array after
passing it to a C library.
By contrast, when a C library returns an array to you, then the memory can not
be managed by NativeCall, and it doesn't know where the array ends. Presumably,
Expand Down

0 comments on commit bfd3f57

Please sign in to comment.