Wednesday, February 17, 2010

realloc / g_realloc woes..

I currently have the following structure;

typedef struct GnoclOption_
const char *optName;
enum GnoclOptionType type;
const char *propName;
gnoclOptFunc *func;
enum GnoclOptionStatus status;
  gboolean b;
  gint i;
  gdouble d;
  gchar *str;
  Tcl_Obj *obj;
  } val;
} GnoclOption;

During compilation, arrays of structures are made. The following is the simplest example:

static GnoclOption drawingAreaOptions[] =
{ "-tooltip", GNOCL_OBJ, "", gnoclOptTooltip },
{ NULL }, 

Usually these are fixed, the dimension of the array defined in the code itself. The GtkDrawingArea widget has no default signals as the process is left to the coder. I want to pass this feature onto the Tcl/Gnocl scripter, so I need to expand the above static declaration as needed.

For instance, I might want to append

{“-motion”, GNOCL_OBJ,””,gnoclOptOnMotion}

to the array. I know that I should use realloc or g_realloc to achieve this task, but my attempts fail. This is the snippet in my source that I’m working on:

/* get the total number of items in the array */
gint n = sizeof (drawingAreaOptions) / sizeof(GnoclOption);
/* allocate more memory to the array to hold one more entry */
drawingAreaOptions = g_realloc( &drawingAreaOptions, (gint) (n+1 * sizeof(struct GnoclOption_)));
drawingAreaOptions[n].optName = “-motion”;
drawingAreaOptions[n].type = GNOCL_OBJ;
drawingAreaOptions[n].propName = "";
drawingAreaOptions[n].func = gnoclOptOnMotion;

When compiled, I get the following error: “incompatible types in assignment”.

What’s going wrong?

No comments: