<refentry id="glib-Byte-Arrays">
<refmeta>
<refentrytitle>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><title>Synopsis</title>
<synopsis>

#include &lt;glib.h&gt;


struct      <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-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);
void        <link linkend="g-byte-array-sort">g_byte_array_sort</link>               (<link linkend="GByteArray">GByteArray</link> *array,
                                             <link linkend="GCompareFunc">GCompareFunc</link> compare_func);
void        <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>
<title>Description</title>
<para>
<link linkend="GByteArray">GByteArray</link> is based on <link linkend="GArray">GArray</link>, to provide arrays of bytes which grow
automatically as elements are added.
</para>
<para>
To create a new <link linkend="GByteArray">GByteArray</link> use <link linkend="g-byte-array-new">g_byte_array_new</link>().
</para>
<para>
To add elements to a <link linkend="GByteArray">GByteArray</link>, use <link linkend="g-byte-array-append">g_byte_array_append</link>(), and
<link linkend="g-byte-array-prepend">g_byte_array_prepend</link>().
</para>
<para>
To set the size of a <link linkend="GByteArray">GByteArray</link>, use <link linkend="g-byte-array-set-size">g_byte_array_set_size</link>().
</para>
<para>
To free a <link linkend="GByteArray">GByteArray</link>, use <link linkend="g-byte-array-free">g_byte_array_free</link>().
</para>

<example>
<title>Using a <structname>GByteArray</structname>.</title>
<programlisting>
  GByteArray *gbarray;
  gint i;

  gbarray = g_byte_array_new (<!>);
  for (i = 0; i < 10000; i++)
    g_byte_array_append (gbarray, (guint8*) "abcd", 4);

  for (i = 0; i < 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>
<title>Details</title>
<refsect2>
<title><anchor id="GByteArray">struct GByteArray</title>
<programlisting>struct GByteArray
{
  guint8 *data;
  guint	  len;
};
</programlisting>
<para>
The <structname>GByteArray</structname> struct allows access to the public fields of a <structname>GByteArray</structname>.
</para><informaltable pgwide="1" frame="none" role="struct">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row>
<entry><link linkend="guint8">guint8</link> *<structfield>data</structfield></entry>
<entry>a pointer to the element data. The data may be moved as elements are
added to the <link linkend="GByteArray">GByteArray</link>.
</entry>
</row>
<row>
<entry><link linkend="guint">guint</link> <structfield>len</structfield></entry>
<entry>the number of elements in the <link linkend="GByteArray">GByteArray</link>.

</entry>
</row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-byte-array-new">g_byte_array_new ()</title>
<programlisting><link linkend="GByteArray">GByteArray</link>* g_byte_array_new                (void);</programlisting>
<para>
Creates a new <link linkend="GByteArray">GByteArray</link>.
</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry>the new <link linkend="GByteArray">GByteArray</link>.


</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-byte-array-sized-new">g_byte_array_sized_new ()</title>
<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">GByteArray</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><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>reserved_size</parameter>&nbsp;:</entry>
<entry>number of bytes preallocated.
</entry></row>
<row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry>the new <link linkend="GByteArray">GByteArray</link>.


</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-byte-array-append">g_byte_array_append ()</title>
<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">GByteArray</link>.
The array will grow in size automatically if necessary.
</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>array</parameter>&nbsp;:</entry>
<entry>a <link linkend="GByteArray">GByteArray</link>.
</entry></row>
<row><entry align="right"><parameter>data</parameter>&nbsp;:</entry>
<entry>the byte data to be added.
</entry></row>
<row><entry align="right"><parameter>len</parameter>&nbsp;:</entry>
<entry>the number of bytes to add.
</entry></row>
<row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry>the <link linkend="GByteArray">GByteArray</link>.


</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-byte-array-prepend">g_byte_array_prepend ()</title>
<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">GByteArray</link>.
The array will grow in size automatically if necessary.
</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>array</parameter>&nbsp;:</entry>
<entry>a <link linkend="GByteArray">GByteArray</link>.
</entry></row>
<row><entry align="right"><parameter>data</parameter>&nbsp;:</entry>
<entry>the byte data to be added.
</entry></row>
<row><entry align="right"><parameter>len</parameter>&nbsp;:</entry>
<entry>the number of bytes to add.
</entry></row>
<row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry>the <link linkend="GByteArray">GByteArray</link>.


</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-byte-array-remove-index">g_byte_array_remove_index ()</title>
<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">GByteArray</link>.
The following bytes are moved down one place.
</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>array</parameter>&nbsp;:</entry>
<entry>a <link linkend="GByteArray">GByteArray</link>.
</entry></row>
<row><entry align="right"><parameter>index</parameter>&nbsp;:</entry>
<entry>the index of the byte to remove.
</entry></row>
<row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry>the <link linkend="GByteArray">GByteArray</link>.


</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-byte-array-remove-index-fast">g_byte_array_remove_index_fast ()</title>
<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">GByteArray</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">GByteArray</link>. But it is faster than
<link linkend="g-byte-array-remove-index">g_byte_array_remove_index</link>().
</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>array</parameter>&nbsp;:</entry>
<entry>a <link linkend="GByteArray">GByteArray</link>.
</entry></row>
<row><entry align="right"><parameter>index</parameter>&nbsp;:</entry>
<entry>the index of the byte to remove.
</entry></row>
<row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry>the <link linkend="GByteArray">GByteArray</link>.


</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-byte-array-sort">g_byte_array_sort ()</title>
<programlisting>void        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 <function><link linkend="qsort">qsort</link>()</function>-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).
</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>array</parameter>&nbsp;:</entry>
<entry>a <link linkend="GByteArray">GByteArray</link>.
</entry></row>
<row><entry align="right"><parameter>compare_func</parameter>&nbsp;:</entry>
<entry>comparison function.


</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-byte-array-sort-with-data">g_byte_array_sort_with_data ()</title>
<programlisting>void        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">g_byte_array_sort</link>(), but the comparison function takes a user data argument.
</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>array</parameter>&nbsp;:</entry>
<entry>a <link linkend="GByteArray">GByteArray</link>.
</entry></row>
<row><entry align="right"><parameter>compare_func</parameter>&nbsp;:</entry>
<entry>comparison function.
</entry></row>
<row><entry align="right"><parameter>user_data</parameter>&nbsp;:</entry>
<entry>data to pass to <parameter>compare_func</parameter>.


</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-byte-array-set-size">g_byte_array_set_size ()</title>
<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">GByteArray</link>, expanding it if necessary.
</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>array</parameter>&nbsp;:</entry>
<entry>a <link linkend="GByteArray">GByteArray</link>.
</entry></row>
<row><entry align="right"><parameter>length</parameter>&nbsp;:</entry>
<entry>the new size of the <link linkend="GByteArray">GByteArray</link>.
</entry></row>
<row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry>the <link linkend="GByteArray">GByteArray</link>.


</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-byte-array-free">g_byte_array_free ()</title>
<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">GByteArray</link>.
If <parameter>free_segment</parameter> is <literal>TRUE</literal> it frees the actual byte data.
</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>array</parameter>&nbsp;:</entry>
<entry>a <link linkend="GByteArray">GByteArray</link>.
</entry></row>
<row><entry align="right"><parameter>free_segment</parameter>&nbsp;:</entry>
<entry>if <literal>TRUE</literal> the actual byte data is freed as well.
</entry></row>
<row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry>


</entry></row>
</tbody></tgroup></informaltable></refsect2>

</refsect1>




</refentry>
