1 <!-- ##### SECTION Title ##### -->
4 <!-- ##### SECTION Short_Description ##### -->
5 arrays of arbitrary elements which grow automatically as elements are added.
7 <!-- ##### SECTION Long_Description ##### -->
9 Arrays are similar to standard C arrays, except that they grow automatically
10 as elements are added.
13 Array elements can be of any size (though all elements of one array are the
14 same size), and the array can be automatically cleared to '0's and
18 To create a new array use g_array_new().
21 To add elements to an array, use g_array_append_val(), g_array_append_vals(),
22 g_array_prepend_val(), and g_array_prepend_vals().
25 To access an element of an array, use g_array_index().
28 To set the size of an array, use g_array_set_size().
31 To free an array, use g_array_free().
34 <title>Using a GArray to store gint values.</title>
39 /* We create a new array to store gint values.
40 We don't want it zero-terminated or cleared to 0's. */
41 garray = g_array_new (FALSE, FALSE, sizeof (gint));
42 for (i = 0; i < 10000; i++)
43 g_array_append_val (garray, i);
45 for (i = 0; i < 10000; i++)
46 if (g_array_index (garray, gint, i) != i)
47 g_print ("ERROR: got %d instead of %d\n",
48 g_array_index (garray, gint, i), i);
50 g_array_free (garray, TRUE);
51 </programlisting></example>
53 <!-- ##### SECTION See_Also ##### -->
58 <!-- ##### STRUCT GArray ##### -->
60 Contains the public fields of an <link linkend="glib-arrays">Array</link>.
63 @data: a pointer to the element data. The data may be moved as elements are
65 @len: the number of elements in the #GArray.
67 <!-- ##### FUNCTION g_array_new ##### -->
69 Creates a new #GArray.
72 @zero_terminated: TRUE if the array should have an extra element at the end
74 @clear: TRUE if #GArray elements should be automatically cleared to '0'
75 when they are allocated.
76 @element_size: the size of each element in bytes.
77 @Returns: the new #GArray.
80 <!-- ##### FUNCTION g_array_sized_new ##### -->
92 <!-- ##### MACRO g_array_append_val ##### -->
94 Adds the value on to the end of the array.
95 The array will grow in size automatically if necessary.
99 g_array_append_val() is a macro which uses a reference to the value
100 parameter @v. This means that you cannot use it with literal values
101 such as "27". You must use variables.
106 @v: the value to append to the #GArray.
107 @Returns: the #GArray.
110 <!-- ##### FUNCTION g_array_append_vals ##### -->
112 Adds @len elements onto the end of the array.
116 @data: a pointer to the elements to append to the end of the array.
117 @len: the number of elements to append.
118 @Returns: the #GArray.
121 <!-- ##### MACRO g_array_prepend_val ##### -->
123 Adds the value on to the start of the array.
124 The array will grow in size automatically if necessary.
127 This operation is slower than g_array_append_val() since the existing elements
128 in the array have to be moved to make space for the new element.
132 g_array_prepend_val() is a macro which uses a reference to the value
133 parameter @v. This means that you cannot use it with literal values
134 such as "27". You must use variables.
139 @v: the value to prepend to the #GArray.
140 @Returns: the #GArray.
143 <!-- ##### FUNCTION g_array_prepend_vals ##### -->
145 Adds @len elements onto the start of the array.
148 This operation is slower than g_array_append_vals() since the existing elements
149 in the array have to be moved to make space for the new elements.
153 @data: a pointer to the elements to prepend to the start of the array.
154 @len: the number of elements to prepend.
155 @Returns: the #GArray.
158 <!-- ##### MACRO g_array_insert_val ##### -->
160 Inserts an element into an array at the given index.
164 g_array_insert_val() is a macro which uses a reference to the value
165 parameter @v. This means that you cannot use it with literal values
166 such as "27". You must use variables.
171 @i: the index to place the element at.
172 @v: the value to insert into the array.
173 @Returns: the #GArray.
176 <!-- ##### FUNCTION g_array_insert_vals ##### -->
178 Inserts @len elements into a #GArray at the given index.
182 @index: the index to place the elements at.
183 @data: a pointer to the elements to insert.
184 @len: the number of elements to insert.
185 @Returns: the #GArray.
188 <!-- ##### FUNCTION g_array_remove_index ##### -->
190 Removes the element at the given index from a #GArray.
191 The following elements are moved down one place.
195 @index: the index of the element to remove.
196 @Returns: the #GArray.
199 <!-- ##### FUNCTION g_array_remove_index_fast ##### -->
201 Removes the element at the given index from a #GArray.
202 The last element in the array is used to fill in the space, so this function
203 does not preserve the order of the #GArray. But it is faster than
204 g_array_remove_index().
208 @index: the index of the element to remove.
209 @Returns: the #GArray.
212 <!-- ##### MACRO g_array_index ##### -->
214 Returns the element of a #GArray at the given index.
215 The return value is cast to the given type.
218 <title>Getting a pointer to an element in a GArray.</title>
220 EDayViewEvent *event;
222 /* This gets a pointer to the 3rd element in the array of EDayViewEvent
224 event = &g_array_index (events, EDayViewEvent, 3);
230 @t: the type of the elements.
231 @i: the index of the element to return.
232 @Returns: the element of the #GArray at the index given by @i.
235 <!-- ##### FUNCTION g_array_set_size ##### -->
237 Sets the size of the array, expanding it if necessary.
238 If the array was created with clear set to TRUE, the new elements are set to 0.
242 @length: the new size of the #GArray.
243 @Returns: the #GArray.
246 <!-- ##### FUNCTION g_array_free ##### -->
248 Frees the memory allocated for the #GArray.
249 If free_segment is TRUE it frees the actual element data as well.
253 @free_segment: if TRUE the actual element data is freed as well.