This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
/
type_conversion.sgml
76 lines (63 loc) · 2.07 KB
/
type_conversion.sgml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!-- ##### SECTION Title ##### -->
Type Conversion Macros
<!-- ##### SECTION Short_Description ##### -->
portably storing integers in pointer variables
<!-- ##### SECTION Long_Description ##### -->
<para>
Many times GLib, GTK+, and other libraries allow you to pass "user
data" to a callback, in the form of a void pointer. From time to time
you want to pass an integer instead of a pointer. You could allocate
an integer, with something like:
<informalexample><programlisting>
int *ip = g_new (int, 1);
*ip = 42;
</programlisting></informalexample>
But this is inconvenient, and it's annoying to have to free the
memory at some later time.
</para>
<para>
Pointers are always at least 32 bits in size (on all platforms GLib
intends to support). Thus you can store at least 32-bit integer values
in a pointer value. Naively, you might try this, but it's incorrect:
<informalexample><programlisting>
gpointer p;
int i;
p = (void*) 42;
i = (int) p;
</programlisting></informalexample>
Again, that example was <emphasis>not</emphasis> correct, don't copy it.
The problem is that on some systems you need to do this:
<informalexample><programlisting>
gpointer p;
int i;
p = (void*) (long) 42;
i = (int) (long) p;
</programlisting></informalexample>
So GPOINTER_TO_INT(), GINT_TO_POINTER(), etc. do the right thing
on the current platform.
</para>
<para>
<warning>
<para>
YOU MAY NOT STORE POINTERS IN INTEGERS. THIS IS NOT PORTABLE IN ANY
WAY SHAPE OR FORM. These macros <emphasis>ONLY</emphasis> allow
storing integers in pointers, and only preserve 32 bits of the
integer; values outside the range of a 32-bit integer will be mangled.
</para>
</warning>
</para>
<!-- ##### SECTION See_Also ##### -->
<para>
</para>
<!-- ##### SECTION Stability_Level ##### -->
<!-- ##### MACRO GSIZE_TO_POINTER ##### -->
<para>
Stuffs a #gsize into a pointer type.
</para>
@s: #gsize to stuff into the pointer.
<!-- ##### MACRO GPOINTER_TO_SIZE ##### -->
<para>
Extracts a #gsize from a pointer. The #gsize must have
been stored in the pointer with GSIZE_TO_POINTER().
</para>
@p: pointer to extract a #gsize from.