J
Jim Showalter
I always thought that it is safe for a function to return a pointer to
static storage. And the following code does compile quietly with:
gcc -pedantic -Wall -o foo foo.c
#include <stdio.h>
static char *foo (int y)
{
static char s[10];
sprintf(s, "%d", y);
return s;
}
int main(void)
{
int y = 999;
printf("y = %s\n", foo(y));
return 0;
}
But 'splint' (the 'lint' program supplied with RedHat Linux)
gives the following complaints:
Splint 3.0.1.6 --- 27 May 2002
foo.c: (in function foo)
foo.c:7:12: Unqualified static storage s returned as implicitly only: s
Static storage is transferred in an inconsistent way. (Use -statictrans to
inhibit warning)
foo.c: (in function main)
foo.c:13:21: New fresh storage (type char *) passed as implicitly temp (not
released): foo(y)
Amemory leak has been detected. Storage allocated locally is not released
before the last reference to it is lost. (Use -mustfreefresh to inhibit
warning)
Finished checking --- 2 code warnings
Is this _really_ a memory leak? Or is splint overly paranoid?
droid
static storage. And the following code does compile quietly with:
gcc -pedantic -Wall -o foo foo.c
#include <stdio.h>
static char *foo (int y)
{
static char s[10];
sprintf(s, "%d", y);
return s;
}
int main(void)
{
int y = 999;
printf("y = %s\n", foo(y));
return 0;
}
But 'splint' (the 'lint' program supplied with RedHat Linux)
gives the following complaints:
Splint 3.0.1.6 --- 27 May 2002
foo.c: (in function foo)
foo.c:7:12: Unqualified static storage s returned as implicitly only: s
Static storage is transferred in an inconsistent way. (Use -statictrans to
inhibit warning)
foo.c: (in function main)
foo.c:13:21: New fresh storage (type char *) passed as implicitly temp (not
released): foo(y)
Amemory leak has been detected. Storage allocated locally is not released
before the last reference to it is lost. (Use -mustfreefresh to inhibit
warning)
Finished checking --- 2 code warnings
Is this _really_ a memory leak? Or is splint overly paranoid?
droid