tags:

views:

12

answers:

1

Hello,

I need in global list in my gtk+ application, i use for it GList:

For example:

I have structure:

typedef struct _data
{
  Glist list;
}Data;

I want to use one copy of the list in the whole program:

I have a function bulid my list:

gboolean build_list()
{
   Data->list = g_list_append(Data->list, "First ");
   Data->list = g_list_append(Data->list, "Second ");
   Data->list = g_list_append(Data->list, "Third ");

   g_list_foreach(Data->list, (GFunc)printf, NULL);
}

After calling this function to display all items from the list:

First Second Third

,but when i try to make it in another function - for example:

void foreach()
{   
    g_list_foreach(Data->list, (GFunc)printf, NULL);
}

I see error in gdb:

*Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 0xb7335700 (LWP 5364)] 0xb765a7d7 in strchrnul () from /lib/i686/cmov/libc.so.6 *

How can i create global list in my application?

Thank you.

+1  A: 
typedef struct _data
{
  Glist list;
} Data;

That makes Data a type (i.e. you can define a variable of type Data by doing `Data data_var;), but in your code you are using it as a variable:

gboolean build_list()
{
   Data->list = g_list_append(Data->list, "First ");

If you want to use it is a variable, you need to change things to:

struct _data
{
  Glist list;
} Data;

When you do that, Data is not a pointer, so you would access it's data like this (notice that you're using . instead of ->):

gboolean build_list() { Data.list = g_list_append(Data.list, "First ");

R Samuel Klatchko