Re: [gtk-list] Re: proper way to clean up



> From: Aaron Walker <amwalker@gate.net>
> 
> Federico Mena Quintero wrote:
> 
> > >  I have a structure that contains all my widgets:
> > >
> > >  typedef struct {
> > >      GtkWidget *window;
> > >      GtkWidget *vbox;
> > >      GtkWidget *hbox;
> > >      GtkWidget *label;
> > >      GtkWidget *button;
> > >  } Window;
> >
> > Normally you don't need pointers to all of the "auxiliary" widgets
> > such as boxes and labels, but that's OK.
> 
> what would I do instead? Just have one generic widget that would be used for
> all of them?
> example:
> 
> typedef struct {
>     GtkWidget *window;
>     GtkWidget *widget;
> } Window;
> 
> then use it like:
> 
> win->widget = gtk_label_new();
> ...
> win->widget = gtk_button_new_with_label();
> etc...
> 
> would this save memory that is malloc'd for this program?



The above is perfectly fine; however, you don't need to save pointers
to all the widgets unless you need to access the widget directly
later.  Once you set up the call backs, and show the widget, you
probably no longer need a pointer to it.  Having said all that, I
would just stay with what you have.


<snip>

> Ok, here is my dilemma now:
> 
> is there a difference between the following g_malloc() calls
> 
> Window *win = g_malloc(sizeof(Window *));
> Window *win = g_malloc(sizeof(Window));
> 
> if there is, should I use one over the other?


Yes, there's a huge difference.  The first one allocates 4 bytes (on
most systems) and will lead to memory problems later.  The second one
is what you want - it allocates enough bytes to hold your Window
struct which is more than 4 bytes.  Looks like you need to read a
little bit about pointers in a C book.

<snip>

Dave



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