Home

GArray

GArray is a dynamic array data structure provided by GLib, the foundational utility library used by GNOME and GTK. It stores elements contiguously in memory and uses a fixed element size defined at creation time, allowing it to hold any plain-old-data type such as integers or structs. The API is designed to be generic, enabling easy storage of various element types without rewriting code for each type.

Creation and memory management: A GArray is created with g_array_new, which takes the size of each element

Operations and access: Common operations include appending values (g_array_append_val, g_array_append_vals), inserting and removing elements (g_array_insert_val, g_array_remove_index),

Variants and usage: GLib also provides GPtrArray for arrays of pointers and GByteArray for raw bytes. GArray

and
flags
related
to
initialization.
The
array
grows
automatically
as
elements
are
appended
or
inserted.
Memory
management
is
handled
by
GLib;
the
caller
can
free
the
array
with
g_array_free,
and
choose
whether
to
also
free
the
underlying
storage
or
just
obtain
a
pointer
to
the
data
by
passing
the
appropriate
flag.
and
resizing
(g_array_set_size).
Elements
are
accessed
via
the
g_array_index
macro,
e.g.,
g_array_index(array,
int,
i).
Sorting
support
is
provided
by
g_array_sort
and
g_array_sort_with_data,
using
a
comparator
function.
is
suitable
for
building
dynamic
lists
where
random
access
and
compact
storage
are
important,
and
it
is
widely
used
within
GLib-based
projects.
When
the
stored
data
is
non-POD
or
requires
complex
ownership
semantics,
developers
may
use
GPtrArray
or
other
container
patterns.