Re: [gtk-list] Re: How to use a gpointer?



after having seen various statements on this topic,
i think it's time to clarify some points.

for GLib to compile on several different platforms, certain
assumptions can be made about its data types.

gint8    sizeof (gint8) = 1 byte, signed
guint8   sizeof (guint8) = 1 byte, unsigned
gint16   sizeof (gint16) = 2 bytes, signed
guint16  sizeof (guint16) = 2 bytes, unsigned
gint32   sizeof (gint32) = 4 bytes, signed
guint32  sizeof (guint32) = 4 bytes, unsigned
gint     is merely an alias for the system's int and guarranteed
         to be >= 4 bytes in size, signed.
guint	 is and unsigned gint.
glong	 is an alias for the system's long and guarranteed to be >=
         sizeof (gint), signed.
gulong	 is an unsigned glong.
gpointer is essentially a void* and guarranteed to be >= sizeof (gint)
         and <= sizeof (glong) in size.

from this follows, that a gint (guint) can be stored in a gpointer and be
retrived from a gpointer without lossage, GLib provides 4 macros for this
task:

gint     an_int;
gpointer a_pointer = GINT_TO_POINTER (an_int);
         an_int = GPOINTER_TO_INT (a_pointer);

guint    an_uint;
gpointer a_pointer = GUINT_TO_POINTER (an_uint);
         an_uint = GPOINTER_TO_UINT (a_pointer);

however, it is not save to store a gpointer in a gint and rely on retrival of
the pointer value without lossage. for this task, longs have to be used:

gpointer a_pointer;
glong    a_long = (glong) a_pointer;
         a_pointer = (gpointer) a_long;

gpointer a_pointer;
gulong   an_ulong = (gulong) a_pointer;
         a_pointer = (gpointer) an_ulong;

---
ciaoTJ



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