Re: OT: Problems with strings



Le lundi 09 mai 2005 �1:36 +0200, Magnus Wirstr� �it :
> m�agen den 9 maj 2005 21.12 skrev Jean Br�rt:
> > Le lundi 09 mai 2005 �1:04 +0200, Magnus Wirstr� �it :
> > > Hi.
> > >
> > > I'm a newbie so please don't fry me :)
> > > I have a strange problem (at least i think it's strange ;) ). In my app i
> > > set up a gchar like this
> > >
> > > 	gchar *post;
> > >
> > > 	entry_widget = lookup_widget(GTK_WIDGET(button), "text_bolag");
> > > 	buffer = gtk_entry_get_text(GTK_ENTRY(entry_widget));
> > > 	strcpy(post,buffer);
> > > 	memset(buffer,0,100);
> > >
> > > When i run it I get segmentation fault and the debugger tell me this
> > > error. post		(gchar *)0x12 <Address 0x12 out of bounds>
> > >
> > > What could generate a such runtime error ? I have no idea why i get this
> > > and it have been working before. I'm thankful for any suggestion.
> >
> > You are using an uninitialized pointer. You should use something as:
> > post = g_new (char, some_size);
> > before calling strcpy. BUT DON'T USE strcpy AT ALL, use strncpy to avoif
> > buffer overflow problems.
> 
> Thank you, that worked good ... should i do this with all my string to make my 
> program safe ? or just gchar * ? Is it a good rule to always use g_new to set 
> up my strings ?
> And of course not forget to g_free() too.

You can also use static string :
gchar post[length];
Or directly use malloc, or any other allocation method.

Please, note that this list is not really the good place for such a
discussion. gtk-list would be much more appropriate, IMHO




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