From: "Shorty Porty" <getshorty_@hotmail.com>
To: 'Raseel Bhagat' <raseelbhagat@gmail.com>,
'C programming list' <linux-c-programming@vger.kernel.org>
Subject: RE: how to implement routines that return general strings?
Date: Sat, 12 Aug 2006 04:36:52 +1000 [thread overview]
Message-ID: <BAY104-DAV4846B1779D88C62F4E816ED4B0@phx.gbl> (raw)
In-Reply-To: <f68850780608110003u78bc0f1albc7551f30599be27@mail.gmail.com>
On 8/11/06, Glynn Clements <glynn@gclements.plus.com> wrote:
> Avoid the second form. Apart from not being re-entrant, it can lead to
> bugs which can be hard to find, particularly if the function in
> question is relatively general-purpose and may be used from various
> places in your code.
char*
func()
{
static buf[128] ; // or whatever size is appropriate
... copy result into buf ...
return buf ;
}
It isn't re-entrant, because of the static buffer. In a process, all threads
will get the same copy of the static buffer, so if one thread calls the
function and hasn't copied the string contents before another threads calls
the function, they will both get the string from the 2nd call. If both
threads are inside that function at the same time, they are both going to be
playing with that buf[] at the same time. Icky.
Shorty
next prev parent reply other threads:[~2006-08-11 18:36 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-08-10 14:10 how to implement routines that return general strings? Robert P. J. Day
2006-08-10 17:12 ` Scott
2006-08-10 19:04 ` David Rientjes
2006-08-11 20:06 ` Kelly Burkhart
2006-08-12 9:36 ` Robert P. J. Day
2006-08-14 18:15 ` Scott
2006-08-10 21:07 ` Glynn Clements
2006-08-11 7:03 ` Raseel Bhagat
2006-08-11 18:36 ` Shorty Porty [this message]
-- strict thread matches above, loose matches on Subject: below --
2006-08-10 16:51 Khan, Ahmer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=BAY104-DAV4846B1779D88C62F4E816ED4B0@phx.gbl \
--to=getshorty_@hotmail.com \
--cc=linux-c-programming@vger.kernel.org \
--cc=raseelbhagat@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).