c - 将 sizeof() 与普通数组和 garray 一起使用

标签 c sizeof glib

这是一个对 GArray 进行排序的练习程序,我使用 sizeof() 来了解数组的大小。

从逻辑上思考,sizeof(x) 应该是 24,即 6 个整数 * 每个整数的大小,即 4 - 6*4。

但是当我将这些整数放入 GArray 时,大小为 8。

为什么是 8,为什么不是 32? .. 因为 g_array_new 以 2 的幂分配字节? 24 附近最接近的 2 的幂是 2^5,即 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);
}

最佳答案

是的,我得到了答案:

GArray是一个结构体 有 2 个元素,其中 1 个为 gchar 类型,另一个为 Gunit 类型

gchar 为 1 字节 Gunit 为 4 个字节

所以 sizeof(GArray) 或 sizeof(arr) 在这里将是 1+4 = 5,然后将其扩展到最接近的 2 的幂,即 2^3 是 8 :)

关于c - 将 sizeof() 与普通数组和 garray 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41501068/

相关文章:

c - 如何用枚举解释和调用函数

c - 如何转换此 "for"循环以在 C 中使用 EOF?

c - 在表中实现字符串文字及其大小

c++ - 指向非 union 类的指针的大小可以不同吗?

ubuntu - Linux系统下获取机器数据

c - "(type)variable"和 "*((type *)&variable)"之间有什么区别(如果有)?

C getchar() 的误解

c++ - 在 C++ 中使用 sizeof 的奇怪行为

c - 如何从 GList 的结构中读取值?

c - 在二进制文件中存储 GList 的良好格式,以便文本编辑器无法识别文本字段的值