Re: new plugin and problem

Bill WIlson <[email protected]>
Newsgroups gmane.comp.gnome.apps.gkrellm
Message-ID <[email protected]>
On Sun, 23 Apr 2006 13:26:19 +0200
pierluigi petrelli <[email protected]> wrote:

> tanks for your fast help, i have solved all my trouble.
> ps. i have tryed to declare input
> 
>  static char input[length];
> 
> but at compile time gcc say me:
> 
> gcc -fPIC `pkg-config gtk+-2.0 --cflags` -c g4mon.c
> g4mon.c: In function `get_cpu_fanspeed':
> g4mon.c:52: error: storage size of 'input' isn't constant
> g4mon.c:52: error: size of variable 'input' is too large
> make: *** [g4mon.o] Error 1

Ok, I was making the point it is not safe to return a pointer
to a buffer that lives on the stack.

You just set length to BUFSIZ, so instead of this:

    const int length = BUFSIZ;
    char input[length];
    if ( fgets( input, length, fp ) ) {
        temp = atof(input);

Just do this:

    static char input[BUFSIZ];
    if ( fgets( input, BUFSIZ, fp ) ) {
        temp = atof(input);

If there is a case where you really need a variable length buffer, you could do
something like this:

   static char *input;

   if (!input)
      input = g_malloc(length);
    if ( fgets( input, length, fp ) ) {
        temp = atof(input);

where the above fragment doesn't show anything at all about the case where
length might change and you want to g_free() and redo the g_malloc().

BIll

_______________________________________________
Gkrellm mailing list
[email protected]
http://ninja.linux-phreak.biz/mailman/listinfo/gkrellm
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.