Use of sizeof() with normal arrays and garrays

409 views Asked by At

it is a practice program to sort GArrays, I have used sizeof() to know the size of my arrays.

Thinking logically, sizeof(x) should be 24 i.e 6 integers * size of each integer i.e 4 - 6*4.

But when I put these integers into my GArray, the size is 8.

why is it 8, why not 32? .. as g_array_new allocates bytes in powers of 2? and nearest power of 2 near 24 is 2^5 i.e 32

/*************************************************************************************************************
*   FILE NAME   :   ex-garray-6.c
*
*   DESCRIPTION :   sort Garray using GCompareFunc (Not used in GIMP, Gaim or Evolution)
*
************************************************************************************************************/

#include<glib.h>
#include<stdio.h>

/*************************************************************************************************************
*   FUNCTION NAME   :       print_arr
*   
*   DESCRIPTION     :       prints entire array using len and g_array_index
*
*   RETURNS         :       void
*
************************************************************************************************************/

void print_arr(GArray* arr)
{
        int i = 0;
        printf("\n Array : \n");
        for (i = 0; i < (arr->len); i++)
        {
                printf("%d\n", g_array_index(arr, int, i));
        }

}

/*************************************************************************************************************
*   FUNCTION NAME   :       compare_ints
*   
*   DESCRIPTION     :       utilized qsort() to sort elements of the unsorted array.
*                           arguments are two gpointers.They are typecasted to int pointers
*                           int the function 
*
*   RETURNS         :       int -  -ve if first arg is smaller than second arg
*                                   0 if first arg is equal to second arg
*                                  +ve - second arg is smaller than first arg
*
************************************************************************************************************/

int compare_ints( gpointer* a, gpointer* b)
{
        int* x = (int*)a;
        int* y = (int*)b;

        return (*x - *y);

}

/*************************************************************************************************************
*   FUNCTION NAME   :       main.c
*   
*   DESCRIPTION     :       main.c declares GArray,allocates memory to it, appends 6 integers into the array,*                           uses g_array_sort to print the array, uses print_arr function to print the array *                           frees array at end.
*
*   RETURNS         :       SUCCESS
*
************************************************************************************************************/

int main(int argc, char** argv)
{

        // 1. declare GArray pointer variable and allocate memory to it
        GArray* arr = g_array_new(FALSE, FALSE, sizeof(int));
        // g_array_set_size(arr,8); - didn't work to fix size to 8 bytes

        // 2. initialize int array of 6 elements say x
        int x[6] = {500,400, 500, 700, 200, 300};

        // 3. append in the array
        arr = g_array_insert_vals(arr,0, x, 6);
        printf("\n size of x : %d \n size of arr : %d", sizeof(x), sizeof(arr));

        // 4. print the array
        print_arr(arr);

        /* 5. sort the array using 
           g_array_sort( 
           <GArray pointer variable>,
           (GCompareFunc)<name of the compare function>);
           - compare function uses qsort()-
           -returns -ve a<b
           -returns 0 a = b
           -returns +ve b = a
           */
        /* 5.5 alternate sorting function -
            g_array_sort_with_data(
            <same as g_array_sort>,
            <same as g_array_sort>,
            <gpointer to user-data>); */

        printf("\n Array after sorting \n ");
        g_array_sort(arr, (GCompareFunc)compare_ints);

        // 6. print garray
        print_arr(arr);

        // 7. free garray 
        g_array_free(arr, TRUE);
}
2

There are 2 answers

0
Quentin On

As said in commentaries, arr is not an array, it is a pointer to a GArray (which is not an array neither, cf GArray reference).

x is an array, so it size is size of an item * number of items, so 6 * sizeof(int).
arr is a pointer, and pointer size depend to the compiler and system used, in your case, a int * has a size of 8.

2
Amninder Singh On

Yes I got the answer:

GArray is a structure having 2 elements 1 of gchar type and other of gunit type

gchar is of 1 byte gunit is of 4 bytes

so sizeof(GArray) or sizeof(arr) here would be 1+4 = 5 then extending it to nearest power of 2 i.e 2^3 is 8 :)