I can't read back the good value from GList
.
I declare a struct
, and initialize its a
variable with a number in the for cycle then add to g_list_append
.
The g_list_nth_data
returns a pointer; I try to cast it to my struct type and read its member, but I get 9.
What am I doing wrong?
#include "glib.h"
#include <stdio.h>
typedef struct MyType
{
int a;
}mytype_t;
int main(int argc, char* argv[])
{
printf("start\n");
mytype_t myt;
GList *l = NULL;
for(int i=0;i<10;i++)
{
myt.a = i;
l = g_list_append(l, &myt);
}
printf("length: %d\n", g_list_length(l));
printf("5th item a value: %d\n", ((mytype_t*)g_list_nth_data(l,4))->a );
return 0;
}
output:
start
length: 10
5th item value: 9
You need to allocate a copy of mytype_t
on the heap for each instance of it which you append to the linked list. Observe the value of &myt
in each g_list_append()
call: it will be identical for each value of i
, since it’s referring to the same chunk of stack memory.
You can do that with:
mytype_t *myt = g_new0 (mytype_t, 1);
myt->a = i;
l = g_list_append (l, myt);
in your loop.
Note that calling g_list_append()
in a loop has a cost of O(N^2), as it has to iterate over the length of the list each time to find the end. In general, using a linked list is a bad idea, and a pointer array (GPtrArray
in GLib) is usually a much more appropriate data structure.