472,992 Members | 3,119 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

safe to return a pointer to static storage?

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
Nov 13 '05 #1
7 6090

"Emmanuel Delahaye" <em**********@noos.fr> wrote in message
news:Xn***************************@130.133.1.4...
In 'comp.lang.c', "Jim Showalter" <ji***********@hotmail.com> wrote:
I always thought that it is safe for a function to return a pointer to
static storage.
It is, but using a static can have strange side effects and is not
recommended as an all-purpose solution.


what are the strange side effects ?

[snip]

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

--
Jeff
Nov 13 '05 #2
In 'comp.lang.c', "Jeff" <no****@notexist.com> wrote:
It is, but using a static can have strange side effects and is not
recommended as an all-purpose solution.


what are the strange side effects ?


int inc (void)
{
static S_i = 0;

S_i++;

return S_i;
}

include <stdio.h>

int main (void)
{
/* the output is unpredictable */
printf ("%d %d\n", inc(), inc());

return 0;
}

even worst with static strings. See strftime() for example.

Not to mention unwanted recursive calls or multithreading.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #3

"Jim Showalter" <ji***********@hotmail.com> wrote in message
news:pa****************************@hotmail.com...
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


I found that the ANSI time function localtime( ), gmtime( ) and mktime( ) also return a pointer to
a static storage structure "tm". I think you are right, it is safe to return a pointer to static
storage.

--
Jeff
Nov 13 '05 #4
> I found that the ANSI time function localtime( ), gmtime( ) and mktime( ) also return a pointer
to
a static storage structure "tm".


I have made a mistake, the function mktime( ) do not return structure tm, it returns time_t.

--
Jeff
Nov 13 '05 #5
In <pa****************************@hotmail.com> "Jim Showalter" <ji***********@hotmail.com> writes:
I always thought that it is safe for a function to return a pointer to
static storage.
It is safe, but it can lead to program bugs, if you're not extremely
careful. Consider the following example:

printf("y = %s, z = %s\n", foo(y), foo(z));

The bug is far from obvious to the non-experienced C programmer.
And the following code does compile quietly with:

gcc -pedantic -Wall -o foo foo.c
As it should. You may also want to add -ansi and -O (-Wall is not
complete without -O), especially for code you intend to post to
this newsgroup :-)
#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)
Nonsensical diagnostic.
foo.c: (in function main)
foo.c:13:21: New fresh storage (type char *) passed as implicitly temp (not
released): foo(y)
Idiotic diagnostic.
Amemory leak has been detected. Storage allocated locally is not released
before the last reference to it is lost. (Use -mustfreefresh to inhibit
warning)
Bullshit.
Finished checking --- 2 code warnings

Is this _really_ a memory leak?
Obviously NOT. Whoever implemented these checks should have his own head
checked.
Or is splint overly paranoid?


Brain dead.

If you want to keep using splint (or any other form of lint), you'll have
to learn how to disable all the checks that only produce garbage output.

In my opinion, a properly invoked gcc removes the need for lint checks.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6
On Mon, 04 Aug 2003 14:13:32 +0000, Dan Pop wrote:
"Jim Showalter" <ji***********@hotmail.com> writes:

[snip]
Or is splint overly paranoid?


Brain dead.

If you want to keep using splint (or any other form of lint), you'll have
to learn how to disable all the checks that only produce garbage output.

In my opinion, a properly invoked gcc removes the need for lint checks.


Well, as I said, I _just_ started using splint and I'm not impressed. You
make a good point. I think I'll just stick to using gcc (with the switches
you and others suggested) and save myself the agro. :)
Thanks again Dan!

Jim Showalter
Nov 13 '05 #7
>Well, as I said, I _just_ started using splint and I'm not impressed. You
make a good point. I think I'll just stick to using gcc (with the switches
you and others suggested) and save myself the agro. :)


Well, I just started playing with it myself. If you add the -weak switch, it
turns off some of its more pedantic stuff. (It appears that -weak enables the
stort of thing that can be checked for without you marking up your code, while
leaving it out makes Splint do all the checks that assume you've marked up your
code.)

E.g.,
splint -weak *.c

I also like the various bool switches: -booltype, -boolfalse and -booltrue,
that make it understand that my BOOL typedef type really means boolean.

Grant D. Watson
gr**********@yahoo.pleasedontspamme.com (Use this one!)
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in...
9
by: JoeC | last post by:
I have a question about advanced poiters. I know the basics of memory managment and pointers. That a pointer points to a memory location. I also have read that dynamic memory new is used for...
3
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think...
1
by: kiluyar | last post by:
I have such a function: class T { public: T(){...//some operations} }; T& GetInst()
3
by: Chris Forone | last post by:
its ok to return *this as reference (C++ FAQ L). but is it also in singleton pattern (ctor example below)? Timer* Timer::sole(0); // static Timer& Timer::Get() // static { return sole ?...
0
by: Anthony Williams | last post by:
James Kanze <james.kanze@gmail.comwrites: 6.7/4 of the C++ Standard describes initialization of local objects with static storage duration: "The zero-initialization (8.5) of all local...
6
by: MN | last post by:
I have 2 questions: 1- Why isn't possible to declare a static char array inside function ? the compiler gives this error: storage size of ‘array’ isn’t constant. 2- Why isn't possible to...
50
by: arunajob | last post by:
Hi all, If I have a piece of code something like this void main(void) { char * p1="abcdefghijklmn"; ............................................. }
41
by: simonl | last post by:
Hi, I've been given the job of sorting out a crash in our product for which we have the crash information and an avi of the event (which can't possibly match but more of that later...) (btw this...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.