Re: GList append problem



Man... :))

You're doing a classical C newbie mistake: buffer is a static (in the
sense that it's not dynamically allocated) variable, a vector of 80
chars (and one of them would be the string terminator, '\0').

The first time, you fill the buffer with "SZJA", and add it to the
GList.  But REMEMBER: the list contains a pointer to char.  A char*.  In
fact, it will contain the value of buffer variable, which is a
pointer.  This value never changes.  Got it?

The second time, you fill THE SAME CHAR VECTOR!  buffer remains the
same, right?  You don't do dynamic memory allocation.  And add it to
GList.  In this moment, glist will contain:

   0x4F532FD3 (the address is for example, not real, of course)
   0x4F532FD3

Ok?  At that address, you will have the new string: "EU HOZZAJARULAS".
Got it?  If not... read a C programming book.

There's no point to explain what happens on subsequent steps.  I will
hive you a solution, though:

  // ... (after you printf the debug information)
  // instead of
  // glist_kinek=g_list_append(glist_kinek,fgets(buffer,80,adat));
  // you should:

  {
    char *tmp;
    fgets(buffer, 80, adat);
    tmp = strdup(buffer); // here we're creating another buffer, to be
                          // stored in list.  this one won't get
                          // overwritten.
    glist_kinek = g_list_append(glist_kinek, tmp);
  }

I don't want to say this is good... it's not, but it works.  Beware that
memory allocated with malloc must be free()-ed at some point, otherwise
your program will end by eating all computer's memory, and will be
killed by the kernel :).

And indent your programs!  Make them look good, not just work.

Cheers,
//mishoo

On Thu, 9 Aug 2001 17:39:02 +0200 (CEST)
deejay westel900 net wrote:

Hi there.

I have a problem with an application I wrote a few days ago. This is a
simple programm which has four combo boxes. The popdown boxes are filled
in with strings from a text file. The text file is simple, every line
contains a string terminated with a '\0' and a '\n'. The first string is
a
name, the second is a price, the third is an id number, the fourth is a
comment. A record consists of this four strings, and a file can hold
unlimited number of this records, terminated with a 0xFF byte. When the
program starts, the corresponding strings from each record is read, and
appended to a GList. This GList will become the content of the popdown
box. The code with some comments:

---------------------------------------------------
#include <gtk/gtk.h>
#include <stdio.h>
#include "interface.h"
#include "support.h"

int main (int argc, char *argv[])
{
        GtkWidget *window1;
        GList *glist_kinek=NULL;
        GList *glist_mennyi=NULL;
        GList *glist_szamlaszam=NULL;
        GList *glist_kozlemeny=NULL;
        FILE *adat;
        int i=0, k=0;
        char buffer[80],buffer2[80],buffer4[80],buffer3[80],allbuff[]=" 
  ";
          gtk_set_locale ();
     gtk_init (&argc, &argv);
  add_pixmap_directory (PACKAGE_DATA_DIR "/pixmaps");
    add_pixmap_directory (PACKAGE_SOURCE_DIR "/pixmaps");
    window1 = create_window1 ();
    adat=fopen("adatbazis","r");
 while(1)
/* This should run until the EOF character.
*/
  {
   glist_kinek=g_list_append(glist_kinek,fgets(buffer,80,adat));
/* This was the line, where we append the string to the glist
*/
          printf("%s",buffer);
/* We print out the appended string to stdout. This is for
verification purposes only.
*/
                  for (i=1;i<4;i++)
                          {       while (1)
                                  {
       if (fgetc(adat) == 0xa) break;
      }
     }
/* This code skips the next tree strings until the next NAME type
 string. The original code did not contain this. I know this is
 very ugly, but I tried to slow down the
 code with unnecessary loops, because I thought this is an 
 optimization bug. It looks like it isn't.
*/
/*      glist_mennyi=g_list_append(glist_mennyi,fgets(buffer2,80,adat));

glist_szamlaszam=g_list_append(glist_szamlaszam,fgets(buffer3,80,adat));
 glist_kozlemeny=g_list_append(glist_kozlemeny,fgets(buffer4,80,adat));

 These are the other pop down boxes. They are commented out, until
 it turns out whats wrong.
*/
          if (fgetc(adat) == 255) break;
                  else fseek(adat,-1,SEEK_CUR);
/* End of file detection. Thats ugly too. Is for slowdown here.
*/
  }
    fclose(adat);
  gtk_combo_set_popdown_strings( GTK_COMBO(combo1), glist_kinek) ;
  gtk_combo_set_popdown_strings( GTK_COMBO(combo2), glist_mennyi) ;
  gtk_combo_set_popdown_strings( GTK_COMBO(combo3), glist_szamlaszam) ;
  gtk_combo_set_popdown_strings( GTK_COMBO(combo4), glist_kozlemeny) ;
  gtk_widget_show (window1);
  gtk_main ();
  return 0;
}

The output in the popdown box is:

OTP NYUGDIJ PENZTAR
OTP NYUGDIJ PENZTAR
OTP NYUGDIJ PENZTAR
OTP NYUGDIJ PENZTAR
OTP NYUGDIJ PENZTAR

Should be:

SZJA
EU HOZZAJARULAS
NYUGDIJ ALAP
EU ALAP
OTP NYUGDIJ PENZTAR

The difference is well visible. The last entry is repeated five times,
and the others are not shown. The read position from the file is good
because the printf shows the good results. The problem is with the GList
I
think. With my knowledge, which comes mainly from The GTK Tutorial the
code should be OK, I can't imagine what could be the problem. This
GLists
are double linked, is it possible that GLib updates the next/previous
pointers badly?

I tried to compile the code on two different computers. On a 1200
T-Bird,
gtk1.2.8,gcc2.95.2 and on a 266Celeron,gtk1.2.6, unknown gcc. I get the
same results.

Thanks for the help in advance.
Hello.
   Gyalokay Szabolcs 


_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


-- 

  ... and on the seventh day, He exited from append mode.




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