Arrays

Arrays — arrays of arbitrary elements which grow automatically as elements are added .

Synopsis

 #include <glib.h> 
GArray; 
GArray* 
g_array_new (
gboolean zero_terminated, 
gboolean clear_, 
guint element_size); 
GArray* 
g_array_sized_new (
gboolean zero_terminated, 
gboolean clear_, 
guint element_size, 
guint reserved_size);
#define 
g_array_append_val (a,v) 
GArray* 
g_array_append_vals (
GArray *array, 
gconstpointer data, 
guint len);
#define 
g_array_prepend_val (a,v) 
GArray* 
g_array_prepend_vals (
GArray *array, 
gconstpointer data, 
guint len);
#define 
g_array_insert_val (a,i,v) 
GArray* 
g_array_insert_vals (
GArray *array, 
guint index_, 
gconstpointer data, 
guint len); 
GArray* 
g_array_remove_index (
GArray *array, 
guint index_); 
GArray* 
g_array_remove_index_fast (
GArray *array, 
guint index_); 
GArray* 
g_array_remove_range (
GArray *array, 
guint index_, 
guint length);
void 
g_array_sort (
GArray *array, 
GCompareFunc compare_func);
void 
g_array_sort_with_data (
GArray *array, 
GCompareDataFunc compare_func, 
gpointer user_data);
#define 
g_array_index (a,t,i) 
GArray* 
g_array_set_size (
GArray *array, 
guint length); 
gchar* 
g_array_free (
GArray *array, 
gboolean free_segment); 

Details


g_array_new ()

GArray*     g_array_new                     (
gboolean zero_terminated, 
gboolean clear_, 
guint element_size);

Creates a new GArray.

zero_terminated : TRUE if the array should have an extra element at the end which is set to 0.
clear_ :TRUE if GArray elements should be automatically cleared to 0 when they are allocated.
element_size :the size of each element in bytes.
Returns :the new GArray.

g_array_sized_new ()

GArray*     g_array_sized_new               (
gboolean zero_terminated, 
gboolean clear_, 
guint element_size, 
guint reserved_size);

Creates a new GArray with reserved_size elements preallocated. This avoids frequent reallocation, if you are going to add many elements to the array. Note however that the size of the array is still 0.

zero_terminated :TRUE if the array should have an extra element at the end with all bits cleared.
clear_ :TRUE if all bits in the array should be cleared to 0 on allocation.
element_size :size of each element in the array.
reserved_size :number of elements preallocated.
Returns :the new GArray.

g_array_append_val()

#define     g_array_append_val(a,v)

Adds the value on to the end of the array. The array will grow in size automatically if necessary.

Note

g_array_append_val() is a macro which uses a reference to the value parameter v. This means that you cannot use it with literal values such as "27". You must use variables.

a :a GArray.
v :the value to append to the GArray.
Returns :the GArray.

g_array_append_vals ()

GArray*     g_array_append_vals             (
GArray *array, 
gconstpointer data, 
guint len);

Adds len elements onto the end of the array.

array :a GArray.
data :a pointer to the elements to append to the end of the array.
len :the number of elements to append.
Returns :the GArray.

g_array_prepend_val()

#define     g_array_prepend_val(a,v)

Adds the value on to the start of the array. The array will grow in size automatically if necessary.

This operation is slower than g_array_append_val() since the existing elements in the array have to be moved to make space for the new element.

Note

g_array_prepend_val() is a macro which uses a reference to the value parameter v. This means that you cannot use it with literal values such as "27". You must use variables.

a :a GArray.
v :the value to prepend to the GArray.
Returns :the GArray.

g_array_prepend_vals ()

GArray*     g_array_prepend_vals            (
GArray *array, 
gconstpointer data, 
guint len);

Adds len elements onto the start of the array.

This operation is slower than g_array_append_vals() since the existing elements in the array have to be moved to make space for the new elements.

array :a GArray.
data :a pointer to the elements to prepend to the start of the array.
len :the number of elements to prepend.
Returns :the GArray.

g_array_insert_val()

#define     g_array_insert_val(a,i,v)

Inserts an element into an array at the given index.

Note

g_array_insert_val() is a macro which uses a reference to the value parameter v. This means that you cannot use it with literal values such as "27". You must use variables.

a :a GArray.
i :the index to place the element at.
v :the value to insert into the array.
Returns :the GArray.

g_array_insert_vals ()

GArray*     g_array_insert_vals             (
GArray *array, 
guint index_, 
gconstpointer data, 
guint len);

Inserts len elements into a GArray at the given index.

array :a GArray.
index_ :the index to place the elements at.
data :a pointer to the elements to insert.
len :the number of elements to insert.
Returns :the GArray.

g_array_remove_index ()

GArray*     g_array_remove_index            (
GArray *array, 
guint index_);

Removes the element at the given index from a GArray. The following elements are moved down one place.

array :a GArray.
index_ :the index of the element to remove.
Returns :the GArray.

g_array_remove_index_fast ()

GArray*     g_array_remove_index_fast       (
GArray *array, 
guint index_);

Removes the element at the given index from a GArray. The last element in the array is used to fill in the space, so this function does not preserve the order of the GArray. But it is faster than g_array_remove_index().

array :a GArray.
index_ :the index of the element to remove.
Returns :the GArray.

g_array_remove_range ()

GArray*     g_array_remove_range            (
GArray *array, 
guint index_, 
guint length);

Removes the given number of elements starting at the given index from a GArray. The following elements are moved to close the gap.

array :a GArray.
index_ :the index of the first element to remove.
length :the number of elements to remove.
Returns :the GArray.

Since 2.4


g_array_sort ()

void        g_array_sort                    (
GArray *array, 
GCompareFunc compare_func);

Sorts a GArray using compare_func which should be a qsort()-style comparison function (returns -1 for first arg is less than second arg, 0 for equal, 1 if first arg is greater than second arg).

array :a GArray.
compare_func :comparison function.

g_array_sort_with_data ()

void        g_array_sort_with_data          (
GArray *array, 
GCompareDataFunc compare_func, 
gpointer user_data);

Like g_array_sort(), but the comparison function receives a user data argument.

array :a GArray.
compare_func :comparison function.
user_data :data to pass to compare_func.


g_array_set_size ()

GArray*     g_array_set_size                (
GArray *array, 
guint length);

Sets the size of the array, expanding it if necessary. If the array was created with clear_ set to TRUE, the new elements are set to 0.

array :a GArray.
length :the new size of the GArray.
Returns :the GArray.

g_array_free ()

gchar*      g_array_free                    (
GArray *array, 
gboolean free_segment);

Frees the memory allocated for the GArray. If free_segment is TRUE it frees the actual element data as well.

array :a GArray.
free_segment :if TRUE the actual element data is freed as well.
Returns :the element data if free_segment is FALSE, otherwise NULL