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 7 6107
"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
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/
"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
> 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
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
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
>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!) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
by: kiluyar |
last post by:
I have such a function:
class T
{
public:
T(){...//some operations}
};
T& GetInst()
|
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 ?...
|
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...
|
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...
|
by: arunajob |
last post by:
Hi all,
If I have a piece of code something like this
void main(void)
{
char * p1="abcdefghijklmn";
.............................................
}
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |