Re: [Vala] using a non-glib C library in vala



Jan Hudec wrote:
What you describe is the meaning of struct. Structs are always allocated
statically and always passed by value (unless ref modifier is used, of
course).

Passing by reference is indicated by declaring as 'class'. Classes are
supposed to support reference-counting, but if they don't, they can be
declared with [Compact] modifier.

However, passing structs as const reference to functions is common
practice in C in order to avoid unnecessary copying. Vala does that by
default, too. Take this code, for example:

----------------------------------------------------
[SimpleType]
struct Point {
        public int x;
        public int y;
}

void print_point (Point p) {
        stdout.printf ("(%d, %d)\n", p.x, p.y);
}

void main () {
        Point p = { 3, 4 };
        print_point (p);
}
----------------------------------------------------

C code without [SimpleType]:

  void print_point (const Point* p);
  ...
    Point p = {3, 4};
    print_point (&p);

C code with [SimpleType]:

  void print_point (Point p);
  ...
    Point p = {3, 4};
    print_point (p);



Best regards,

Frederik



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