Vala Memory Management

I am going to call the Vala function from C, and I have a question about memory management. The function looks like this in Vala:

int[] GetNumbers();

and translates to valacin C like this

gint* GetNumbers(int* result_length1);

When the called function is called from C, is the caller responsible for freeing the array gint*?

+5
source share
1 answer

Question Vala! How amazing!

Vala has a useful parameter -Cthat allows you to look into the C code that it creates. This function, for example ...

int[] GetNumbers() {
    return new int[] {1,2,3};
}

... when compiling with ...

valac -C -c test.vala

... will show the following C code (c test.c) ...

gint* GetNumbers (int* result_length1) {
    gint* result = NULL;
    gint* _tmp0_ = NULL;
    gint* _tmp1_;
    result = (_tmp1_ = (_tmp0_ = g_new0 (gint, 3), _tmp0_[0] = 1, _tmp0_[1] = 2, _tmp0_[2] = 3, _tmp0_), *result_length1 = 3, _tmp1_);
    return result;
}

Pay attention to g_new0; so yes you want g_freeit.

, , , C: const, .

+6

All Articles