[Vala] Problem with arrays passed as reference [was: unexpected error while compiling a vala file]



On 2010-08-20 09:30, Mildred Ki'Lya wrote:
Hi,

I'm having troubles to compile the vala file attached to this e-mail.
The command line is:

    valac --pkg=gio-2.0 --pkg=glib-2.0 email.vala

When the lines containing `buffer += c` are commented out (lines 67 and
70), the program compiles.


I investigated a little bit (I also work on a compiler that generated C
code: Lisaac) and I think I understand the error.

The problem is that Vala arrays (say uint8[] for example) are a
collection of three variables:

uint8* buffer  the pointer to the data
int    length  the number of useful items in the array
int    size    the actual size allocated for the buffer

And because Vala tries to keep a usable interface with C programs, when
such arrays are passed to a function, only the buffer and the length are
passed, and not the size.

So ... When I use the '+=' operator on the array, this operator needs
the size to determine if it needs to reallocate the buffer. But the size
variable doesn't exist. And the compiler doesn't know what to do. it
crashes.

Now, I found a workaround: I declare a local variable for this array,
allocate a new buffer of size 0. I wanted to avoid this allocation, but
the resize had the same effect anyway (I assume that g_renew will shrink
the buffer anyway).

I can append to the local buffer for it has a local size variable. And
at the end, I assign the local buffer to the array given as argument.


But, will this work?
--------------------

I mean, the buffer passed as parameter must have a size variable
somewhere. It was not passed as an argument to the function, but it
should still exist.

Because I reallocated the buffer, the size variable should now be
completely invalid. And using the '+=' operator on the array outside the
function will reallocate the array based on the wrong size. Possibly
causing crashes.

Answer: no, the compiler still crashes.

Ok, I'm going to create a good old buffer object that will hide all of
this ... :)


Could you look at this?
Thanks


Mildred

-- 
Mildred Ki'Lya
╭───────── mildred593@online.fr ──────────
│ Jabber, GoogleTalk: <mildred jabber fr>
│ Website: <http://ki.lya.online.fr>           GPG ID: 9A7D 2E2B
│ Fingerprint: 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 9A7D 2E2B

Attachment: 0x9A7D2E2B.asc
Description: application/pgp-keys

Attachment: signature.asc
Description: OpenPGP digital signature



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]