Re: [gtk-list] Re: Heisenberg was right?



Rouat manu wrote:
> char *convert_to_char(double data)
> {
>   char *c;
>   static char buffer[10];
> 
>   sprintf (buffer, "%g", data);
>   if(data==N_A)
>     sprintf (buffer, "N/A");
>     c=buffer;
>     /* printf("buffer:%g\n",data); */
> 
>   return c;
> }

 Ooops. Returning the address of the stack is a bad idea. Something along
the lines of:

 if(data==N_A)
    sprintf(buffer, "N/A");
 else
    c=strdup(buffer);

 return c;

 Is a better idea. strdup copies the string & takes care of memory
allocation. Also, seeing as the function is small, there is no problem
allocating a lot more than 10 bytes for the buffer.

Kate


-- 
 "I am Grey. I stand between the candle and the star.
    We are Grey. We stand between the darkness and the light."

John "Kate" Looney, Horizon Open Systems. Sun Microsystems distributor and 
Support centre. Hotline: [+353 1 8055700] Web http://www.hos.horizon.ie/



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