Re: [Vala] using a non-glib c library



On Sat, Sep 26, 2009 at 11:42:01 -0500, Mr. Maxwell . wrote:
One more thing, a lot of the functions return C style strings, (char *) are
they the same as vala strings? (so I can replace char * with string?)

Yes, "string" in vala corresponds to "char *" in C. You will just have to
find out which strings you have to free and which not and mark the later as
"weak". All const ones will be weak, but some others might too.

Could you elaborate on the weak strings and when to use them a little? I don't have much experience with C. 
                                    

Actually, it should be "unowned", not "weak". They are currently synonyms,
but weak will mean something slightly different in future.

Unowned variable is one that you are not supposed to deallocate.
Owned variable is one, that you are supposed to deallocate.

In other words, if you have function f, that returns string:

   char *f(...)

Than if the user is supposed to do:

   char *x = f(...);
   ...
   free(x);

than the variable is owned. If the user is not supposed to call free, than
it's is unowned.

Since you can't call free on a pointer to const, any function declared like

   const char *f(...)

will almost surely have to be declared in vala like

   unowned string f(...);

byt functions that return non-const char * may be either, though they will
return owned values more often than unowned.

Note, that owned is default for return values so return values should not be
marked with "owned".

On the other hand for arguments unowned is the default, so they are marked
with "owned" if they are. "owned" argument means that the function eventually
calls free on the passed value.

-- 
                                                 Jan 'Bulb' Hudec <bulb ucw cz>



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