Re: [Vala] Allocate memory for a struct pointer



Hello,

                  في ث، 06-12-2011 عند 16:26 +0100 ، كتب Guenther Niess:
Hi,
I'm quite new and have a problem to initialize a struct. In the
tutorial and faq there are ways, but I think I have to manage the
memory manually in order to use libxml.

Here is an example of allocating the struct manually:

public struct MyStruct {
        int i;
        int j;
}

public void main () {
        MyStruct* str = malloc (sizeof (MyStruct));
        *str = MyStruct () {i = 1, j = 2};

        print ("%d %d\n", str->i, str->j);
        delete str;
}

I'm not sure about your use case, but if you only need this struct
pointer inside a function, you can probably simply allocate your struct
normally and pass its address.
        MyStruct str = ...;
        some_function (&str);

HTH,
Abderrahim




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