`out` fixed-sized arrays generate invalid code
The following Vala code
void my_out_func(out int array[4]) {
array = {1, 2, 3, 4};
}
Is compiled to
void
my_out_func (gint* array[4])
{
gint _vala_array[4] = {0};
gint _tmp0_[4] = {0};
_tmp0_[0] = 1;
_tmp0_[1] = 2;
_tmp0_[2] = 3;
_tmp0_[3] = 4;
memcpy (_vala_array, _tmp0_, 4 * sizeof (gint));
if (array) {
*array = _vala_array;
}
}
I don't do enough raw C to confidently say what's going on here, but the practical result is that, if given an array, it stores a pointer to now-gone stack-alllocated array at its beginning.
int arr[4];
my_out_func(out arr);
print("%i %i %i %i\n", arr[0], arr[1], arr[2], arr[3]); // On 64-bit LE machine: big number, smaller number, 0, 0
int[] arr;
my_out_func(out arr); // Memory corruption, program crashes; vala also tries to set `arr.length` to -1 post-call
out
fixed-sized arrays should use heap allocation regardless - otherwise, they will be lost with stack frame when function returns.
P.S.: passing fixed-length array to variable-length out
array parameter actually works pretty well, aside from the fact that it exhibits behavior from #470.