Re: [Vala] Static array initialization
- From: "Cayle Graumann" <cayle graumann gmail com>
- To: vala paldo org
- Subject: Re: [Vala] Static array initialization
- Date: Thu, 8 Mar 2007 17:48:39 -0600
Speaking of array initialization - I came across an oddity the other
day in version 0.0.6 in that the following syntax would not compile.
I have not tried it with 0.0.7 yet as I am still trying to change over
my code to the new constructor syntax to test other things yet.
function x (int buffersize) {
int[] i;
if(buffersize>10) {
i = new int[100];
} else {
i = new int[10];
}
}
The vala compiler complained about the "int[] i" not being valid.
I eventually was able to work around it by doing this which is of
questionable efficiency:
function x(int buffersize) {
var i = new int[1];
if(buffersize>10) {
i.resize(100);
} else {
i.resize(10);
}
}
I would assume that my first try should evaluate to something like this
int *i;
if(buffersize>10) {
i = ourmalloc(100*sizeof(int));
} else {
i = ourmalloc(10*sizeof(int));
}
where ourmalloc is whatever function we are using to allocate memory.
Is there another way of doing this that I am just missing, or is this a bug?
I like the fact that we can now resize arrays, but wonder at how
efficient an implementation it is. I have some code I'm porting that
extensively uses a linked list (C# ArrayList) of integer arrays to
allow efficient array growth, and if the new resizable arrays are good
enough then I might not worry about reworking that code.
Cayle, Missouri.
On 3/8/07, Dominique Würtz <housirer gmx de> wrote:
First of all, thank you for your help, your code works for me and
declaring the array as const is certainly more appropriate here.
However, I'd like to point out that C allows initialization of arrays of
any type, f.e.
int main (int argc, char ** argv)
{
struct {
int a;
char *b;
} x[] = {
{ 1, "111" },
{ 2, "222" }
};
return 0;
}
runs perfectly, and you can declare x[] as static, const or whatever.
Thinking about it, what reason is there to not allow initialization of
non-const arrays?
Cheers,
Dominique
Raffaele Sandrini schrieb:
> Ok this one is tricky ;). The point is that the programm above is
> incorrect. You are not allowed to create non const arrays of structs.
> This is also not allowed in C. Nevertheless valac does not complain
> about that (as of many other things) ;).
>
> The correct array initialisation would be:
> const Gtk.ActionEntry[] entries = {
> { "FileMenu", null, "_File", null, null, null },
> { "EditMenu", null, "_Edit", null, null, null },
> null
> };
>
> have fun
> Raffaele
>
_______________________________________________
Vala mailing list
Vala paldo org
http://www.paldo.org/mailman/listinfo/vala
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]