分段错误:g_ptr_array_foreach

时间:2017-01-06 11:33:02

标签: c glib

任务3:我正在使用g_ptr_array_foreach来打印GPtrArray ...按照我的意思......这就是我们这样做的方式......但是我仍然会遇到分段错误..请问......有什么见解同行吗? :)

/************************************************************************************************************
*       FILE NAME       :       ex-garray-7.c
*
*       DESCRIPTION     :       demonstrates implementation of pointer arrays i.e GPtrArray
*
************************************************************************************************************/


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

#define SUCCESS 0

/************************************************************************************************************
*       FILE NAME       :       main
*
*       DESCRIPTION     :       allocates memory to pointer array, pointers point to strings 
*
*       RETURNS         :       SUCCESS
*
***********************************************************************************************************/

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

/**********************************NEW LEARNING************************************************************
- all glib functions are perfectly alright
- */

//gpointer ret_val = NULL;

/* GPtrArray is designed to hold pointers, 
  - no need to specify  particular type when    creating it or adding    and indexing elements. */

/******************************REQUIRED DECLARATIONS*******************************************************/

// declare GPtrArray pointer variable
GPtrArray* ptrarr = NULL;


/********************************REQUIREDD INITIALIZATIONS*************************************************/

// allocate memory to GPtrArray using   g_ptr_array_new(); 
ptrarr = g_ptr_array_new(); /* No argument while allocating memory to array of pointers


/*********************************PERFORMING REQUIRED TASKS************************************************/

/* 1. add string hello to it using
    g_ptr_array_add(
                      GPtrArray* array,
                      g_strdup(<"string">)); */
g_ptr_array_add(
                ptrarr,
                g_strdup("Let's"));

// 2. add other strings like I love C and gpointers too
g_ptr_array_add(
                ptrarr,
                g_strdup("Use"));

g_ptr_array_add(
                ptrarr,
                g_strdup("GLib"));


g_ptr_array_add(
                ptrarr,
                g_strdup("\n"));


/* 3. print entire GPtrArray using 
    void g_ptr_array_foreach(
                                 GPtrArray* array,
                                 GFunc function,
                                 gpointer user_data); */
g_ptr_array_foreach(
                    ptrarr,
                    (GFunc)printf,
                    NULL);

printf("\n Removing the third element i.e index is 2");

/* 4. remove the third element using 
   gpointer g_ptr_array_remove_index(
                                        GPtrArray* array,
                                        guint index_
                                        ); */
 g_ptr_array_remove_index(ptrarr, 0);



/* 5. remove the new 3rd and 2nd element
    gpointer g_ptr_array_remove_range(
                                        GPtrArray* array,
                                        guint index_,
                                        guint length
                                      ); */
g_ptr_array_remove_range(ptrarr, 0,1);

// 6. print the new GPtrArray
printf("\n pointer array now is \n ");
g_ptr_array_foreach(
                    ptrarr,
                    (GFunc)printf,
                    NULL);

// 7.  print the first item in GPtrArray
printf("\n The first element in the GPtrArray is %s ", g_ptr_array_index(ptrarr, 0)/* same as g_array_index*/);

// 8.  free the memory
g_ptr_array_free(ptrarr, TRUE); // same as g_array_free
return SUCCESS;

}

还有其他方法可以打印GPtrarray的元素吗?

2 个答案:

答案 0 :(得分:3)

glib foreach函数的第二个参数必须与以下原型匹配:

void funcname (gpointer data, gpointer user_data);

printf与原型不匹配。如果你删除(GFunc)printf中的强制转换,编译器会告诉你这个。使用强制转换隐藏消息告诉您代码中的错误并不是一个好主意。

因此,您需要使用包装函数:

g_ptr_array_foreach (ptrarr, print_cb, NULL);

static void
print_cb (gpointer data,
          gpointer user_data)
{
  printf ("%s\n", data);
}

或者,更好的是,只需循环内联以使事情更清晰并节省多次调用回调的开销:

for (gsize i = 0; i < ptrarr->len; i++)
  {
    const gchar *element = g_ptr_array_index (ptrarr, i);
    printf ("%s\n", element);
  }

答案 1 :(得分:-1)

glib 参考文档中,原型为:

g_ptr_array_foreach (GPtrArray *array,
                     GFunc func,
                     gpointer user_data);

您的代码几乎正确。与其强制转换printf,不如尝试使用g_print

g_ptr_array_foreach(
                    ptrarr,
                    g_print,
                    NULL);