Re: Example in Docs: GPOINTER_TO_INT



On Wednesday, May 30, 2001, at 08:28  PM, Brooklyn Linux Solutions wrote:

I'm sorry  to take  this into a digression, why would we want to
cast a pointer, which has a memory address stored in it, to an integer?

There are many APIs in GNOME that let you store a pointer. For example, callback functions get a pointer passed through to them, and GLists hold pointers in each list element.

When you want to put an integer in one of these places instead of a pointer. You use GINT_TO_POINTER to do it. You then use GPOINTER_TO_INT to turn the integer back. It's not portable to use GPOINTER_TO_INT to change an arbitrary pointer into an integer.

This is a nice shortcut. If this wasn't available, a more heavyweight way to do the same thing would be to allocate storage and put the integer into it. But then you'd have to free the storage when you're done. So it's nice to be able to use these interfaces that use gpointer with simple integers. If you prefer not to use GINT_TO_POINTER, you won't ever be forced to use it.

    -- Darin




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