Segmentation fault : g_ptr_array_foreach

1.2k views Asked by At

Task 3: I am using g_ptr_array_foreach to print the GPtrArray ... As per to me..this is how we do it.. but still I am getting segmentation fault.. Please.. Any insight fellow peers? :)

/************************************************************************************************************
*       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;

}

is there some other way as well, to print the elements of the GPtrarray?

2

There are 2 answers

5
M.M On

The second argument to the glib foreach functions must match the following prototype:

void funcname (gpointer data, gpointer user_data);

printf does not match that prototype. If you remove the cast in (GFunc)printf the compiler will tell you this. It is not a good idea to use casts to hide messages telling you what the bug is in your code.

Consequently, you need to use a wrapper function:

g_ptr_array_foreach (ptrarr, print_cb, NULL);

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

Or, better yet, just do the loop inline to make things clearer and save the overhead of calling the callback many times:

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

From glib reference document, the prototype is:

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

Your code is almost correct. Instead of casting printf, try to use g_print

g_ptr_array_foreach(
                    ptrarr,
                    g_print,
                    NULL);