Re: Problem with GtkCList, dynamically creating with N columns...



On Tue, 06 Mar 2001, Wrinkled Shirt wrote:
> Hey again,
> 
> The original problem again was dynamically creating an array of blank 
> strings to be used as an argument in creating a gtkclist widget. The point 
> was to create some code that could replace:
> 
> /**/
> char *blankrow[] = {"", "", ""};  // needed for three columns
> /**/
> 
> ...with something else that would build dynamically, assuming I already knew 
> the value of the number of columns needed.
> 
> Based on Flavio's response (thanks Flavio), I did the following...

I hate to tell you this, but if Flavio recommended this he should be shot.

> /**/
> 
> int *blankrow;

That should be char **blankrow.

> int ctr;
> 
> blankrow = (int *)malloc(vpsql->cols);  // vpsql->cols = # of cols

That should be blankrow = (char **) malloc(vpsql->cols * sizeof(*blankrow));

> for (ctr=0; ctr<vpsql->cols; ctr++)
> {
>   (char *)blankrow[ctr] = (char *)malloc(2);
>   sprintf((char *)blankrow[ctr]," ");

These two lines could be blankrow[ctr] = strdup(""), although:

    blankrow[ctr] = (char *)malloc(2);
    sprintf(blankrow[ctr]," ");

is also legal C it doesn't do quite what you asked for (it sets every title to
space rather than to an empty string).

> }
> 
> vpsql->grid = gtk_clist_new_with_titles(vpsql->cols, (char **)blankrow);

Having fixed the above, you can drop the cast.

> /**/
> 
> There's a lot of extra casting that I threw in there because of compile 
> warnings, but the effect is pretty much the same with or without them. The 
> thing works (thank god), although there are a couple of things that trouble 
> me.

All those warnings was the compiler trying to tell you that something
was wrong.

> 1. If I substitute the declarations, mallocs and for loop with the 
> hard-coded alternative above, I get no runtime error messages. However, with 
> the new declarations, mallocs and for loop, I get the following error 
> message repeated quite a lot. I cannot quite figure out why...

Since you've completely corrupted the heap, I'm surprised anything works.

> 2. My C skills are really rudimentary, so I have no idea how to free up the 
> memory I've malloced up there. I've tried a few things, but each time I do 
> it I get an error. Considering that I'm asking for a very limited amount of 
> memory, it's tempting to just let it go, but I suppose that wouldn't be 
> proper.

Free in reverse order:

for (ctr=0; ctr<vpsql->cols; ctr++)
    free(blankrow[ctr]);
free(blankrow);

Cheers,

-- 
Ali Harlow                              Email: ali avrc city ac uk
Research programmer                     Tel:   (020) 7477 8000 X 4348
Applied Vision Research Centre          Intl: +44 20 7477 8000 X 4348
City University                         Fax:   (020) 7505 5515
London                                  Intl: +44 20 7505 5515




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]