GArrays를 정렬하는 연습 프로그램입니다. sizeof()를 사용하여 배열 크기를 알았습니다.sizeof()의 일반 배열과 garray 사용
논리적으로 생각하면 sizeof (x)는 24, 즉 6 정수 여야합니다. * 각 정수의 크기는 4 - 6 * 4입니다.
하지만 내 GArray에 이러한 정수를 둘 때, 크기가, 8
는 왜 8입니다 왜 안 32? .. g_array_new는 2의 거듭 제곱에서 바이트를 할당하기 때문에? 2 (24) 근처의 가장 가까운 전력이 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);
}
'sizeof (arr)'는'arr '에있는 요소의 수가 아니라 포인터의 크기로 평가됩니다. –
... 배열은 포인터가 아닙니다. – haccks
똑바로는 안되지만 확실히 관련이 있습니다. (http://stackoverflow.com/questions/33836247/how-can-i-find-the-length-of-given-garray). – George