linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Glynn Clements <glynn.clements@virgin.net>
To: "Linux-C-Programming (E-mail)" <linux-c-programming@vger.kernel.org>
Subject: Re: Stupid Question.
Date: Thu, 13 May 2004 19:27:15 +0100	[thread overview]
Message-ID: <16547.48643.913168.610550@cerise.nosuchdomain.co.uk> (raw)
In-Reply-To: <20040513143852.GE1912@lug-owl.de>


Jan-Benedict Glaw wrote:

> Static automatic variables (those that are within functions) do have a
> different semantic: their contents is preserved during multiple function
> calls.

"Static automatic variables" is a self-contradiction. "Static local
variables" (as opposed to "automatic local variables") would be more
accurate.

Local variables (those whose scope is limited to the block in which
they are declared) are automatic (stored on the stack) by default. The
"static" keyword makes them static (stored in the data segment).

This overloading of the "static" keyword is a common source of
confusion. They really should have added another keyword, IMHO. [And
dispensed with the (entirely redundant) "automatic" keyword, which
I've never actually seen used in nearly 20 years of C programming.]

And the distinction between automatic and static local variables is
somewhat deeper than the fact that the value is preserved across
calls. It's highly significant when dealing with reentrancy (e.g. 
recursive functions, multi-threaded code).

For instance, an automatic variable has a separate instance for each
call, while a static variable only has a single instance. OTOH, you
can legitimately return or store a pointer to a static variable and
reference it from outside of the function, whereas an automatic
variable ceases to exist once the function returns.

-- 
Glynn Clements <glynn.clements@virgin.net>

      reply	other threads:[~2004-05-13 18:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-12 18:53 Stupid Question John T. Williams
2004-05-12 19:02 ` Jeff Woods
2004-05-13 14:38   ` Jan-Benedict Glaw
2004-05-13 18:27     ` Glynn Clements [this message]

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=16547.48643.913168.610550@cerise.nosuchdomain.co.uk \
    --to=glynn.clements@virgin.net \
    --cc=linux-c-programming@vger.kernel.org \
    /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).