Re: sprintf and clists



>
> > gchar buffer[1024];
> > static gchar *cclist[1];
> >
> > sprintf(buffer, "%d", some_dumb_int);
> > cclist[0] = strdup(buffer);
> > gtk_clist_append(GTK_CLIST(clist1), cclist);
> >
> > This is assuming you have a 1 column clist.
> >
>
> check out if the string is '\0' terminated. best initialize it after the
> declaration with memset(buffer,'\0',1024);

I don't think you need to check for the '\0' as the buffer is large enough
to hold an integer. An integer might be at max 32bits, so for an unisgned
int, approx. 4 000 000 000 (4 billion) or 4.0e9. So there is a maximum of 10
digits or caracteres. As the buffer is 1024 large, it is surelly big enough
not to risk to have a buffer overflow.

> BTW wouldn't it be enough to use a buffer of size "sizeof(int)" ?

No you cannot do that. Sizeof(int) give the size in octet allocate for one
integer, depending on the compiler and the machine, it might be 2 or 4
octets. If you declare your buffer as sizeof(int), then at max you have a
table of 4 caracteres. So you cannot put 1000 for example into such a
buffer, it's too big number (it's 4 digits! so there is no place left for
the '\0')

Jean-Christophe






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