<refentry id="glib-Byte-Arrays">
<refmeta>
<refentrytitle role="top_of_page" id="glib-Byte-Arrays.top_of_page">Byte Arrays</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo>GLIB Library</refmiscinfo>
</refmeta>

<refnamediv>
<refname>Byte Arrays</refname>
<refpurpose>arrays of bytes, which grow automatically as elements are added</refpurpose>
</refnamediv>

<refsynopsisdiv id="glib-Byte-Arrays.synopsis" role="synopsis">
<title role="synopsis.title">Synopsis</title>

<synopsis>

#include &lt;glib.h&gt;

                    <link linkend="GByteArray">GByteArray</link>;
<link linkend="GByteArray">GByteArray</link>*         <link linkend="g-byte-array-new">g_byte_array_new</link>                    (void);
<link linkend="GByteArray">GByteArray</link>*         <link linkend="g-byte-array-sized-new">g_byte_array_sized_new</link>              (<link linkend="guint">guint</link> reserved_size);
<link linkend="GByteArray">GByteArray</link> *        <link linkend="g-byte-array-ref">g_byte_array_ref</link>                    (<link linkend="GByteArray">GByteArray</link> *array);
<link linkend="void">void</link>                <link linkend="g-byte-array-unref">g_byte_array_unref</link>                  (<link linkend="GByteArray">GByteArray</link> *array);
<link linkend="GByteArray">GByteArray</link>*         <link linkend="g-byte-array-append">g_byte_array_append</link>                 (<link linkend="GByteArray">GByteArray</link> *array,
                                                         const <link linkend="guint8">guint8</link> *data,
                                                         <link linkend="guint">guint</link> len);
<link linkend="GByteArray">GByteArray</link>*         <link linkend="g-byte-array-prepend">g_byte_array_prepend</link>                (<link linkend="GByteArray">GByteArray</link> *array,
                                                         const <link linkend="guint8">guint8</link> *data,
                                                         <link linkend="guint">guint</link> len);
<link linkend="GByteArray">GByteArray</link>*         <link linkend="g-byte-array-remove-index">g_byte_array_remove_index</link>           (<link linkend="GByteArray">GByteArray</link> *array,
                                                         <link linkend="guint">guint</link> index_);
<link linkend="GByteArray">GByteArray</link>*         <link linkend="g-byte-array-remove-index-fast">g_byte_array_remove_index_fast</link>      (<link linkend="GByteArray">GByteArray</link> *array,
                                                         <link linkend="guint">guint</link> index_);
<link linkend="GByteArray">GByteArray</link>*         <link linkend="g-byte-array-remove-range">g_byte_array_remove_range</link>           (<link linkend="GByteArray">GByteArray</link> *array,
                                                         <link linkend="guint">guint</link> index_,
                                                         <link linkend="guint">guint</link> length);
<link linkend="void">void</link>                <link linkend="g-byte-array-sort">g_byte_array_sort</link>                   (<link linkend="GByteArray">GByteArray</link> *array,
                                                         <link linkend="GCompareFunc">GCompareFunc</link> compare_func);
<link linkend="void">void</link>                <link linkend="g-byte-array-sort-with-data">g_byte_array_sort_with_data</link>         (<link linkend="GByteArray">GByteArray</link> *array,
                                                         <link linkend="GCompareDataFunc">GCompareDataFunc</link> compare_func,
                                                         <link linkend="gpointer">gpointer</link> user_data);
<link linkend="GByteArray">GByteArray</link>*         <link linkend="g-byte-array-set-size">g_byte_array_set_size</link>               (<link linkend="GByteArray">GByteArray</link> *array,
                                                         <link linkend="guint">guint</link> length);
<link linkend="guint8">guint8</link>*             <link linkend="g-byte-array-free">g_byte_array_free</link>                   (<link linkend="GByteArray">GByteArray</link> *array,
                                                         <link linkend="gboolean">gboolean</link> free_segment);
</synopsis>
</refsynopsisdiv>









<refsect1 id="glib-Byte-Arrays.description" role="desc">
<title role="desc.title">Description</title>
<para>
<link linkend="GByteArray"><type>GByteArray</type></link> is based on <link linkend="GArray"><type>GArray</type></link>, to provide arrays of bytes which grow
automatically as elements are added.
</para>
<para>
To create a new <link linkend="GByteArray"><type>GByteArray</type></link> use <link linkend="g-byte-array-new"><function>g_byte_array_new()</function></link>.
</para>
<para>
To add elements to a <link linkend="GByteArray"><type>GByteArray</type></link>, use <link linkend="g-byte-array-append"><function>g_byte_array_append()</function></link>, and
<link linkend="g-byte-array-prepend"><function>g_byte_array_prepend()</function></link>.
</para>
<para>
To set the size of a <link linkend="GByteArray"><type>GByteArray</type></link>, use <link linkend="g-byte-array-set-size"><function>g_byte_array_set_size()</function></link>.
</para>
<para>
To free a <link linkend="GByteArray"><type>GByteArray</type></link>, use <link linkend="g-byte-array-free"><function>g_byte_array_free()</function></link>.
</para>
<example>
<title>Using a <structname>GByteArray</structname></title>
<programlisting>
  GByteArray *gbarray;
  gint i;
  gbarray = g_byte_array_new (<!-- -->);
  for (i = 0; i &lt; 10000; i++)
    g_byte_array_append (gbarray, (guint8*) "abcd", 4);
  for (i = 0; i &lt; 10000; i++)
    {
      g_assert (gbarray->data[4*i] == 'a');
      g_assert (gbarray->data[4*i+1] == 'b');
      g_assert (gbarray->data[4*i+2] == 'c');
      g_assert (gbarray->data[4*i+3] == 'd');
    }
  g_byte_array_free (gbarray, TRUE);
</programlisting></example>
</refsect1>

<refsect1 id="glib-Byte-Arrays.details" role="details">
<title role="details.title">Details</title>
<refsect2 id="GByteArray" role="struct">
<title>GByteArray</title>
<indexterm zone="GByteArray"><primary sortas="ByteArray">GByteArray</primary></indexterm><programlisting>typedef struct {
  guint8 *data;
  guint	  len;
} GByteArray;
</programlisting>
<para>
The <structname>GByteArray</structname> struct allows access to the public fields of a <structname>GByteArray</structname>.
</para><variablelist role="struct">
<varlistentry>
<term><link linkend="guint8">guint8</link>&#160;*<structfield>data</structfield>;</term>
<listitem><simpara>a pointer to the element data. The data may be moved as elements are
added to the <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><link linkend="guint">guint</link>&#160;<structfield>len</structfield>;</term>
<listitem><simpara>the number of elements in the <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem>
</varlistentry>
</variablelist></refsect2>
<refsect2 id="g-byte-array-new" role="function">
<title>g_byte_array_new ()</title>
<indexterm zone="g-byte-array-new"><primary sortas="byte_array_new">g_byte_array_new</primary></indexterm><programlisting><link linkend="GByteArray">GByteArray</link>*         g_byte_array_new                    (void);</programlisting>
<para>
Creates a new <link linkend="GByteArray"><type>GByteArray</type></link> with a reference count of 1.
</para><variablelist role="params">
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>the new <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-byte-array-sized-new" role="function">
<title>g_byte_array_sized_new ()</title>
<indexterm zone="g-byte-array-sized-new"><primary sortas="byte_array_sized_new">g_byte_array_sized_new</primary></indexterm><programlisting><link linkend="GByteArray">GByteArray</link>*         g_byte_array_sized_new              (<link linkend="guint">guint</link> reserved_size);</programlisting>
<para>
Creates a new <link linkend="GByteArray"><type>GByteArray</type></link> with <parameter>reserved_size</parameter> bytes preallocated. This
avoids frequent reallocation, if you are going to add many bytes to
the array. Note however that the size of the array is still 0.
</para><variablelist role="params">
<varlistentry><term><parameter>reserved_size</parameter>&#160;:</term>
<listitem><simpara>number of bytes preallocated.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>the new <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-byte-array-ref" role="function" condition="since:2.22">
<title>g_byte_array_ref ()</title>
<indexterm zone="g-byte-array-ref" role="2.22"><primary sortas="byte_array_ref">g_byte_array_ref</primary></indexterm><programlisting><link linkend="GByteArray">GByteArray</link> *        g_byte_array_ref                    (<link linkend="GByteArray">GByteArray</link> *array);</programlisting>
<para>
Atomically increments the reference count of <parameter>array</parameter> by one. This
function is MT-safe and may be called from any thread.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>array</parameter>&#160;:</term>
<listitem><simpara> A <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> The passed in <link linkend="GByteArray"><type>GByteArray</type></link>.

</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.22</para></refsect2>
<refsect2 id="g-byte-array-unref" role="function" condition="since:2.22">
<title>g_byte_array_unref ()</title>
<indexterm zone="g-byte-array-unref" role="2.22"><primary sortas="byte_array_unref">g_byte_array_unref</primary></indexterm><programlisting><link linkend="void">void</link>                g_byte_array_unref                  (<link linkend="GByteArray">GByteArray</link> *array);</programlisting>
<para>
Atomically decrements the reference count of <parameter>array</parameter> by one. If the
reference count drops to 0, all memory allocated by the array is
released. This function is MT-safe and may be called from any
thread.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>array</parameter>&#160;:</term>
<listitem><simpara> A <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.22</para></refsect2>
<refsect2 id="g-byte-array-append" role="function">
<title>g_byte_array_append ()</title>
<indexterm zone="g-byte-array-append"><primary sortas="byte_array_append">g_byte_array_append</primary></indexterm><programlisting><link linkend="GByteArray">GByteArray</link>*         g_byte_array_append                 (<link linkend="GByteArray">GByteArray</link> *array,
                                                         const <link linkend="guint8">guint8</link> *data,
                                                         <link linkend="guint">guint</link> len);</programlisting>
<para>
Adds the given bytes to the end of the <link linkend="GByteArray"><type>GByteArray</type></link>.
The array will grow in size automatically if necessary.
</para><variablelist role="params">
<varlistentry><term><parameter>array</parameter>&#160;:</term>
<listitem><simpara>a <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&#160;:</term>
<listitem><simpara>the byte data to be added.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>len</parameter>&#160;:</term>
<listitem><simpara>the number of bytes to add.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>the <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-byte-array-prepend" role="function">
<title>g_byte_array_prepend ()</title>
<indexterm zone="g-byte-array-prepend"><primary sortas="byte_array_prepend">g_byte_array_prepend</primary></indexterm><programlisting><link linkend="GByteArray">GByteArray</link>*         g_byte_array_prepend                (<link linkend="GByteArray">GByteArray</link> *array,
                                                         const <link linkend="guint8">guint8</link> *data,
                                                         <link linkend="guint">guint</link> len);</programlisting>
<para>
Adds the given data to the start of the <link linkend="GByteArray"><type>GByteArray</type></link>.
The array will grow in size automatically if necessary.
</para><variablelist role="params">
<varlistentry><term><parameter>array</parameter>&#160;:</term>
<listitem><simpara>a <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&#160;:</term>
<listitem><simpara>the byte data to be added.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>len</parameter>&#160;:</term>
<listitem><simpara>the number of bytes to add.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>the <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-byte-array-remove-index" role="function">
<title>g_byte_array_remove_index ()</title>
<indexterm zone="g-byte-array-remove-index"><primary sortas="byte_array_remove_index">g_byte_array_remove_index</primary></indexterm><programlisting><link linkend="GByteArray">GByteArray</link>*         g_byte_array_remove_index           (<link linkend="GByteArray">GByteArray</link> *array,
                                                         <link linkend="guint">guint</link> index_);</programlisting>
<para>
Removes the byte at the given index from a <link linkend="GByteArray"><type>GByteArray</type></link>.
The following bytes are moved down one place.
</para><variablelist role="params">
<varlistentry><term><parameter>array</parameter>&#160;:</term>
<listitem><simpara>a <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>index_</parameter>&#160;:</term>
<listitem><simpara>the index of the byte to remove.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>the <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-byte-array-remove-index-fast" role="function">
<title>g_byte_array_remove_index_fast ()</title>
<indexterm zone="g-byte-array-remove-index-fast"><primary sortas="byte_array_remove_index_fast">g_byte_array_remove_index_fast</primary></indexterm><programlisting><link linkend="GByteArray">GByteArray</link>*         g_byte_array_remove_index_fast      (<link linkend="GByteArray">GByteArray</link> *array,
                                                         <link linkend="guint">guint</link> index_);</programlisting>
<para>
Removes the byte at the given index from a <link linkend="GByteArray"><type>GByteArray</type></link>.
The last element in the array is used to fill in the space, so this function
does not preserve the order of the <link linkend="GByteArray"><type>GByteArray</type></link>. But it is faster than
<link linkend="g-byte-array-remove-index"><function>g_byte_array_remove_index()</function></link>.
</para><variablelist role="params">
<varlistentry><term><parameter>array</parameter>&#160;:</term>
<listitem><simpara>a <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>index_</parameter>&#160;:</term>
<listitem><simpara>the index of the byte to remove.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>the <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-byte-array-remove-range" role="function" condition="since:2.4">
<title>g_byte_array_remove_range ()</title>
<indexterm zone="g-byte-array-remove-range" role="2.4"><primary sortas="byte_array_remove_range">g_byte_array_remove_range</primary></indexterm><programlisting><link linkend="GByteArray">GByteArray</link>*         g_byte_array_remove_range           (<link linkend="GByteArray">GByteArray</link> *array,
                                                         <link linkend="guint">guint</link> index_,
                                                         <link linkend="guint">guint</link> length);</programlisting>
<para>
Removes the given number of bytes starting at the given index from a
<link linkend="GByteArray"><type>GByteArray</type></link>.  The following elements are moved to close the gap.
</para><variablelist role="params">
<varlistentry><term><parameter>array</parameter>&#160;:</term>
<listitem><simpara>a <parameter>GByteArray</parameter>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>index_</parameter>&#160;:</term>
<listitem><simpara>the index of the first byte to remove.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>length</parameter>&#160;:</term>
<listitem><simpara>the number of bytes to remove.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>the <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.4</para></refsect2>
<refsect2 id="g-byte-array-sort" role="function">
<title>g_byte_array_sort ()</title>
<indexterm zone="g-byte-array-sort"><primary sortas="byte_array_sort">g_byte_array_sort</primary></indexterm><programlisting><link linkend="void">void</link>                g_byte_array_sort                   (<link linkend="GByteArray">GByteArray</link> *array,
                                                         <link linkend="GCompareFunc">GCompareFunc</link> compare_func);</programlisting>
<para>
Sorts a byte array, using <parameter>compare_func</parameter> which should be a <link linkend="qsort"><function>qsort()</function></link>-style
comparison function (returns less than zero for first arg is less than second
arg, zero for equal, greater than zero if first arg is greater than second
arg).
</para>
<para>
If two array elements compare equal, their order in the sorted array is
undefined.
</para><variablelist role="params">
<varlistentry><term><parameter>array</parameter>&#160;:</term>
<listitem><simpara>a <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>compare_func</parameter>&#160;:</term>
<listitem><simpara>comparison function.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-byte-array-sort-with-data" role="function">
<title>g_byte_array_sort_with_data ()</title>
<indexterm zone="g-byte-array-sort-with-data"><primary sortas="byte_array_sort_with_data">g_byte_array_sort_with_data</primary></indexterm><programlisting><link linkend="void">void</link>                g_byte_array_sort_with_data         (<link linkend="GByteArray">GByteArray</link> *array,
                                                         <link linkend="GCompareDataFunc">GCompareDataFunc</link> compare_func,
                                                         <link linkend="gpointer">gpointer</link> user_data);</programlisting>
<para>
Like <link linkend="g-byte-array-sort"><function>g_byte_array_sort()</function></link>, but the comparison function takes an extra user data
argument.
</para><variablelist role="params">
<varlistentry><term><parameter>array</parameter>&#160;:</term>
<listitem><simpara>a <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>compare_func</parameter>&#160;:</term>
<listitem><simpara>comparison function.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>user_data</parameter>&#160;:</term>
<listitem><simpara>data to pass to <parameter>compare_func</parameter>.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-byte-array-set-size" role="function">
<title>g_byte_array_set_size ()</title>
<indexterm zone="g-byte-array-set-size"><primary sortas="byte_array_set_size">g_byte_array_set_size</primary></indexterm><programlisting><link linkend="GByteArray">GByteArray</link>*         g_byte_array_set_size               (<link linkend="GByteArray">GByteArray</link> *array,
                                                         <link linkend="guint">guint</link> length);</programlisting>
<para>
Sets the size of the <link linkend="GByteArray"><type>GByteArray</type></link>, expanding it if necessary.
</para><variablelist role="params">
<varlistentry><term><parameter>array</parameter>&#160;:</term>
<listitem><simpara>a <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>length</parameter>&#160;:</term>
<listitem><simpara>the new size of the <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>the <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-byte-array-free" role="function">
<title>g_byte_array_free ()</title>
<indexterm zone="g-byte-array-free"><primary sortas="byte_array_free">g_byte_array_free</primary></indexterm><programlisting><link linkend="guint8">guint8</link>*             g_byte_array_free                   (<link linkend="GByteArray">GByteArray</link> *array,
                                                         <link linkend="gboolean">gboolean</link> free_segment);</programlisting>
<para>
Frees the memory allocated by the <link linkend="GByteArray"><type>GByteArray</type></link>.
If <parameter>free_segment</parameter> is <link linkend="TRUE--CAPS"><literal>TRUE</literal></link> it frees the actual byte data. If the reference
count of <parameter>array</parameter> is greater than one, the <link linkend="GByteArray"><type>GByteArray</type></link> wrapper is preserved but
the size of <parameter>array</parameter> will be set to zero.
</para><variablelist role="params">
<varlistentry><term><parameter>array</parameter>&#160;:</term>
<listitem><simpara>a <link linkend="GByteArray"><type>GByteArray</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>free_segment</parameter>&#160;:</term>
<listitem><simpara>if <link linkend="TRUE--CAPS"><literal>TRUE</literal></link> the actual byte data is freed as well.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>the element data if <parameter>free_segment</parameter> is <link linkend="FALSE--CAPS"><literal>FALSE</literal></link>, otherwise <link linkend="NULL--CAPS"><literal>NULL</literal></link>.
        The element data should be freed using <link linkend="g-free"><function>g_free()</function></link>.
</simpara></listitem></varlistentry>
</variablelist></refsect2>

</refsect1>




</refentry>
