Re: dynamic memory for everything?



On Thu, 31 Jul 2008 16:31:31 +0200 Martin (OPENGeoMap) wrote:

Hi


http://library.gnome.org/devel/glib/2.17/glib-Date-and-Time-Functions.html#GDate 


I have a doubt about how they are working all classes in the glib.

Is it always uses dynamic memory for everything?


GDate*
g_date_new (void)
{
 GDate *d = g_new0 (GDate, 1); /* happily, 0 is the invalid flag for 
everything. */

 return d;
}



That is the kind of data which GData in principle could be used
either as a variable.

What advantages are obtained using dynamic memory in GData or other 
simple structs with only integers and not related with gobject system?

In this case, you actually can use GDate statically:

{
    GDate date;

    g_date_clear(&date, 1);
    g_date_set_time_t(time(NULL));
}

In general, using allocated memory rather than static structs can allow
for some amount of opacity.  The actual structure of the struct can be
hidden away in an implementation file and changed at will, without
breaking existing programs.  GHashTable could be a good example of
this.  There are several ways of implementing a hash table, and perhaps
one day someone will decide that a different implementation is faster,
or requires less memory.  But, possibly it requires the GHashTable
struct to have more members.  If GHashTable were public, this wouldn't
be possible without breaking the library's ABI.  A side-effect of this
is that you can't allocate one statically.

I imagine there are other reasons as well, but this was the first that
came to mind.

        -brian



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