Hi, if you compile and run the attached trivial test program, this will be the output:
$ ./test
element.data = 42, array[23].data = 0
$
If you look at the intermediate C-code, it's clear what happens:
70 foo_bar (&element);
71 _tmp2_ = array[23];
72 foo_bar (&_tmp2_);
73 fprintf (stdout, "element.data = %d, array[23].data = %d\n",
element.data, array[23].data);
So while a single variable will be directly passed (by reference) to the
modifying function, an element of an array will first be copied to a
temporary variable and a reference to that temporary variable will be
passed along. Since only the temporary variable will be modified this
won't have any affect on the array.
Unless this is the intended behavior, I think vala shouldn't copy
structs before passing them to some function when they are elements of
an array. At the very least, the behavior should be consistent
regardless whether it's an array of structs or a single struct.
If this is already fixed or if I'm just too stupid to write correct
code, just tell me so!
Best regards
Alexander Kurtz
struct Foo {
int data;
public void bar(){
data = 42;
}
}
void main(){
Foo element = { 0 };
Foo[] array = new Foo[100];
element.bar();
array[23].bar();
stdout.printf("element.data = %d, array[23].data = %d\n", element.data, array[23].data);
}
/* test.c generated by valac 0.12.1, the Vala compiler
* generated from test.vala, do not modify */
#include <glib.h>
#include <glib-object.h>
#include <string.h>
#include <stdio.h>
#define TYPE_FOO (foo_get_type ())
typedef struct _Foo Foo;
struct _Foo {
gint data;
};
GType foo_get_type (void) G_GNUC_CONST;
Foo* foo_dup (const Foo* self);
void foo_free (Foo* self);
void foo_bar (Foo *self);
void _vala_main (void);
void foo_bar (Foo *self) {
(*self).data = 42;
}
Foo* foo_dup (const Foo* self) {
Foo* dup;
dup = g_new0 (Foo, 1);
memcpy (dup, self, sizeof (Foo));
return dup;
}
void foo_free (Foo* self) {
g_free (self);
}
GType foo_get_type (void) {
static volatile gsize foo_type_id__volatile = 0;
if (g_once_init_enter (&foo_type_id__volatile)) {
GType foo_type_id;
foo_type_id = g_boxed_type_register_static ("Foo", (GBoxedCopyFunc) foo_dup, (GBoxedFreeFunc) foo_free);
g_once_init_leave (&foo_type_id__volatile, foo_type_id);
}
return foo_type_id__volatile;
}
void _vala_main (void) {
Foo _tmp0_ = {0};
Foo element;
Foo* _tmp1_ = NULL;
Foo* array;
gint array_length1;
gint _array_size_;
Foo _tmp2_;
_tmp0_.data = 0;
element = _tmp0_;
_tmp1_ = g_new0 (Foo, 100);
array = _tmp1_;
array_length1 = 100;
_array_size_ = 100;
foo_bar (&element);
_tmp2_ = array[23];
foo_bar (&_tmp2_);
fprintf (stdout, "element.data = %d, array[23].data = %d\n", element.data, array[23].data);
array = (g_free (array), NULL);
}
int main (int argc, char ** argv) {
g_type_init ();
_vala_main ();
return 0;
}
signature.asc
Description: This is a digitally signed message part
_______________________________________________ vala-list mailing list [email protected] http://mail.gnome.org/mailman/listinfo/vala-list
