Re: Glib problem: trying to use gpointer pointing to a structure in a GList. Getting segfault



Hi Albert:

	I was misunderstood. I did this :)

thanks a lot!
Lucas Brasilino


it doesn't help ( all items in list will be the same )
you have to :

   struct RECORD {
         guint index;
         gchar *data;
     } *record;

record = (struct RECORD*)g_malloc(sizeof(struct RECORD))
record->data = strdup("aaa");
dlist = g_list_append(dlist, record);

record = (struct RECORD*)g_malloc(sizeof(struct RECORD))
record->data = strdup("bbb");
dlist = g_list_append(dlist, record);

now it should work.

albert pala


your record is pointer.
you have to allocate structure record and then use pointer to it.
         struct {
                 guint index;
                 gchar *data;
         } x, *record;
record = &x;
record->data = "blabla1"

albert pala


====== cut here ===============

#include <stdio.h>
#include <string.h>
#include <glib.h>

int main(int argc, char **argv)
{

         GList *dlist = NULL, *list = NULL;
         guint length,i;
         struct {
                 guint index;
                 gchar *data;
         } *record;

         record->data = g_malloc(strlen("Lucas")+1);
         g_strlcpy(record->data,"Lucas",strlen("Lucas")+1);
         dlist = g_list_append(dlist, record);

         record->data = g_malloc(strlen("Brasilino")+1);
         g_strlcpy(record->data,"Brasilino", strlen("Brasilino")+1);
         dlist = g_list_append(dlist, record);

         length = g_list_length(dlist);

         g_printf("%d \n",length);

         record = NULL;
         list = g_list_first(dlist);
         while(list)
         {
                 record = list->data;
                 g_print("%s\n",record->data);
                 list = list->next;
     }
}

_______________________________________________
gtk-list mailing list
gtk-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-list
_______________________________________________
gtk-list mailing list
gtk-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-list



--

[]'s
Lucas Brasilino
brasilino recife pe gov br
http://www.recife.pe.gov.br
Emprel -	Empresa Municipal de Informatica (pt_BR)
		Municipal Computing Enterprise (en_US)
Recife - Pernambuco - Brasil
Fone: +55-81-34167078




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