On Sun, 2005-08-07 at 21:18 -0700, Yogesh M wrote: > i used gtk gtk_label_new( const char *str ) to create > a new label[why it needs a const char?] > > below is a part of my code
WoW ! What about "The C programming language" book ? It seems you need it badly :) > > const ch; No type. > const char *cch; > ch==cch; It's a comparision. You wanted c=cch; > strcpy(ch,"i put some thing here"); When did you allocated space for this ? >From the strcpy man page (`man strcpy`): "If the destination string of a strcpy() is not large enough (that is, if the programmer was stupid/lazy, and failed to check the size before copying) then anything might happen. Overflowing fixed length strings is a favourite cracker technique." Please no offence for the hard words - I bet you're not stupid nor lazy - but you must allocate some space before to use strcpy. #define THE_STRING "i put something here" #define BUFFER_SIZE 1024 int i; char ch[BUFFER_SIZE]; i = strlen(THE_STRING); if( i >= 1024 ){ printf("Not enough space to copy the string\n"); }else{ strcpy(ch, THE_STRING); } > label=gtk_label_new(cch); Ok, let's see .... you can do it statically: char ch[1024]; strcpy(ch,"i put something here"); It's dangerous as you can easily overflow the 1024 char buffer. You can also do it dinamically with a fixed length: char* ch; ch = (char*) malloc(sizeof(char) * 1024); if( ch ){ strcpy(ch,"i put something here"); free(ch); } The same apply, you can overflow the buffer. But you need to reserve statically (1st example) or dinamically (2nd example) some memory before to store nothing. What you want is simply: label=gtk_label_new("i put something here"); Or even: const char* ch = "i put something here"; label=gtk_label_new(ch); Or even: char *ch; ch = g_strdup("i put something here"); label=gtk_label_new((const char*)ch); g_free(ch); > ok, it works first time, miraculous ... I did not expect it even to run. > but when the program executes > this function again it creates a segmentation fault. Not surprising at all :) > also > label=gtk_label_new((const char*)ch); > doesnt work either, Yes, it works. The bug is on allocating memory for strcpy - better, on not allocating it at all - among others. > I get segmentation fault when, i destroy the dialog > which contain the label. You may get segfault at any random moment as you're copying a string to memory without allocating space for it, so you may be overwritting memory areas that may be used by your program. I'd like to recommend you to improve your C a bit, before to jump to "advanced" topics as Gtk programming, because it will be a real pain to learn it without a deep knoweledge of the C programming language. To build command line applications until you understand in deep memory management, pointers and operators, will help a lot. I also recommend you to read this tiny document (45 pages) where all the pitfalls you made are described clearly: http://cslibrary.stanford.edu/101/EssentialC.pdf "basic types" at pag. 3 "= is not ==" at pag. 9 "strcpy" at pag. 21 "pointer type effects" (casting) at page 35. Hope this helps. -- Iago Rubio _______________________________________________ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list