Re: [Vala] Vala : Suggestion for improvement : String Class wrapper for strings in Vala : Addendum
- From: Serge Hulne <serge hulne gmail com>
- To: vala-list <vala-list gnome org>
- Subject: Re: [Vala] Vala : Suggestion for improvement : String Class wrapper for strings in Vala : Addendum
- Date: Tue, 21 Jun 2011 20:18:35 +0200
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 ?
Serge.
On Tue, Jun 21, 2011 at 8:02 PM, Serge Hulne <serge hulne gmail com> wrote:
Erratum:
=======
As Alexandre Rosenfeld correctly pointed out, the easy (and correct)
way to get a reference on a Vala string is simply to declare it as
follows:
string a = "hello";
string * b = a;
As illustrated in the following snippet:
///
void main (string[] argv) {
string a = "hello";
string * b = a;
stdout.printf("Value of a = %s\n", a);
stdout.printf("Value of b = %s\n", b);
}
///
Which yields :
Value of a = hello
Value of b = hello
the corresponding C code generated by Vala being:
///----------------
/* test_ref.c generated by valac 0.12.1, the Vala compiler
* generated from test_ref.vala, do not modify */
#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define _g_free0(var) (var = (g_free (var), NULL))
void _vala_main (gchar** argv, int argv_length1);
void _vala_main (gchar** argv, int argv_length1) {
gchar* _tmp0_;
gchar* a;
const gchar* b;
_tmp0_ = g_strdup ("hello");
a = _tmp0_;
b = a;
fprintf (stdout, "Value of a = %s\n", a);
fprintf (stdout, "Value of b = %s\n", b);
_g_free0 (a);
}
int main (int argc, char ** argv) {
g_type_init ();
_vala_main (argv, argc);
return 0;
}
///---------
In which there is indeed no duplication of memory allocation.
as can be seen from:
_tmp0_ = g_strdup ("hello");
a = _tmp0_;
b = a;
Serge.
On Tue, Jun 21, 2011 at 9:05 AM, Serge Hulne <serge hulne gmail com>
wrote:
Suggestion:
=========
A class wrapper for the string type in Vala, in order to avoid
duplication of data in assignments (which would yield an unnecessarily
huge memory usage when processing a large amount of text with a lot of
assignments).
if you compare the C code generated by Vala for:
1)
///
class String {
public string s;
}
void main (string[] argv) {
String a = new String();
a.s = "hello";
String b = a;
stdout.printf("a.s = %s\n", a.s);
stdout.printf("b.s = %s\n", b.s);
}
///
To :
2) the C code generated by Vala for:
///
void main (string[] argv) {
string a = "hello";
string b = a;
stdout.printf("a = %p\n", &a);
stdout.printf("b = %p\n", &b);
}
///
You will notice that in the second case (the naive straightforward
formulation) the data is duplicated (passed by value in the
assignment) whereas in the first case (which uses a class wrapper) the
object a is assigned to b by reference (not by value, i.e. the data is
not duplicated)
Serge.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]