Re: Out of scope and a function



On Fri, Jun 01, 2007 at 09:42:35PM +0000, DC A wrote:
> Hi! I'm having trouble explaining a code. My application has a top window 
> which
> contains a GtkList widget.

Please use GtkTreeView instead of GtkList.

> GList *fill_list_with_items(GtkWidget *list)
> {  int i;
>    GList *list_items;
>    list_items = NULL;
>    for(i = 1; i <= 50; i++)
>    {
>       list_items = g_list_prepend(list_items,
>             populate_row(list, i));
>    }
>    list_items = g_list_reverse(list_items);
> 
>    gtk_list_append_items(GTK_LIST(list), list_items);
>    return list_items;
> }
> 
> populate_row() function create GtkListItem for each row and populate with 
> vbox which
> contains a GtkLabel widget. My question is: in the above code GList 
> *list_items
> does not exist beyond the function fill_list_with_items(). So how come the 
> compiler
> doesn not complain about it of getting out of scope?

Variables are getting out of scope all the time, that's not
the thing the compiler warns about.  Do you think it should
warn that in

  int
  foo(void)
  {
      int i = 5;
      return i;
  }

i is getting out of scope?

No.  The compiler emits warning such as

  source.c:123: warning: function returns address of local variable

if you made avaiable the address of something that went out
of scope.  This does not happen here.  The function returns
list_items and list_items is the address of some memory
allocated on heap.  Only returning &list_items would be
a problem.

Yeti


--
http://gwyddion.net/



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