On Tue, 29 Sep 2015, Sven Schreiber wrote:
I run into problems (=error) when I try to pass a bundle element as a
function argument in pointerized form. Minimal script:
<hansl>
function void hey(matrix *hu)
print "hey"
end function
bundle bbb
bbb.in = ones(2,2)
hey(&bbb.in)
</hansl>
I also tried "&(bbb.in)" (with parentheses), with no luck. I can
actually understand why pointing to something inside a bundle would
be problematic, but OTOH since we want to do lots of stuff with
bundles and still avoid copying large chunks of data, I'm wondering
what the recommended solution would be.
Gretl is actually pretty clever at this (though no doubt the doc
should be made clearer).
First of all, it's right that you're not allowed to "take the address
of" an object inside a bundle; that's by design. However:
(1) When you reference a matrix inside a bundle, in the first instance
gretl simply records the matrix pointer (no full copy). If it then
turns out that you're assigning this matrix under a distinct name,
then it must be copied in full, but if it's just being used as
intermediate input it's not copied. For example
# case 1: 'm' is a full copy of bun.mat
matrix m = bun.mat
# case 2: 'm' is a new matrix but bun.mat is used as an argument
# without being copied.
matrix m = bun.mat'
(2) When a matrix is used as an argument to a user-defined function,
there are two ways to ensure it is "passed along" to the function
rather than being copied: give the argument in "pointer" form, or
mark the argument as "const".
So consider a modified version of your script:
<hansl>
function void hey(const matrix hu)
print "hey"
end function
bundle bbb
bbb.in = ones(2,2)
hey(bbb.in)
</hansl>
The matrix bbb.in will not be copied in this case. But if we removed
the const flag to the "hu" argument it would get copied on entry to
the function.
Allin