Re: [Vala] Vala : Suggestion for improvement : String Class wrapper for strings in Vala : Erratum (2)



I stand corrected !

Thank you Abderrahim for this detailed explanation.

Serge.



On Tue, Jun 21, 2011 at 8:56 PM, Abderrahim Kitouni <a kitouni gmail com>wrote:

Hello,

على 21 يون, 2011 م 07:18, كتب Serge Hulne:

 Apparently

  string a = "hello";
  string * b = a;

and :

   string a = "hello";
   unowned string  b = a;

appear to be translated by Vala into the exact same C code.

Are these two ways to declare the same thing or is this just a bug which
happens to work perchance ?


Not exactly the same thing, but the difference is relatively subtle:
unowned means that the variable doesn't "own" the reference, that is, it
doesn't need to be freed when the variable goes out of scope; a pointer,
however, means that vala doesn't do the memory management at all, it's the
*programmer's* responsibility to free the reference (or not) before the
variable goes out of scope (using the delete statement).

For example, with
       unowned string b = a.dup ();
the compiler will report an error, while with
       string* b = a.dup ();
it won't, and you need to explicitly
       delete b;
after you're done with it.

Generally, if you're optimizing your program to reduce copying, it's better
to use an unowned reference rather than a pointer.

HTH,
Abderrahim



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