Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: initialization of local static variable |
| Date | 2011-10-07 15:56 +0000 |
| Organization | Freie Universitaet Berlin |
| Message-ID | <9f8lokFi5lU1@mid.uni-berlin.de> (permalink) |
| References | <16f1e150-05c8-4e30-a0e3-17aa3182f14d@d28g2000vby.googlegroups.com> <chine.bleu-1373B4.03030507102011@news.eternal-september.org> <j6mkhb$m4s$1@dont-email.me> <9f852eF700U1@mid.uni-berlin.de> <j6mp4b$h3d$1@dont-email.me> |
BartC <bc@freeuk.com> wrote:
> >> (I'm assuming p_first is re-initialised on each entry to the function.)
> >
> > No, 'p_first', being a static variable, is initialized exactly
> > once when the program is loaded - at least that's how I would
> > tend to interpret 3.1.2.4 in C89:
> I tried this:
> int a=1901;
> void testfn(void) {
> static int b=a;
> printf("A, B = %d, %d\n",a,b);
> }
> - int main(void) {
> - testfn();
> - a=2901;
> - testfn();
> - }
> [hyphens added to avoid weird formatting issues in Windows Mail]
> Which worked as expected, with lccwin32 compiler (b is assigned the value
> of a on each entry to the function).
That, for me, would be absolutely unexpected since then the
'static' qualifier on 'b' would have no effect at all, it
would behave exactly the same as if you would have written
int b = a;
> On other, presumably more compliant compilers, this initialisation of b
> wasn't allowed.
>
> However the behaviour allowed by lccwin32 is more
> intuitive and more useful. (There is nothing after all to stop you writing:
> static int b; b=a;
> which would have the same effect. So why not the convenience of having it
> all in one statement?)
As I said, I would be rather surprised by this behaviou since I'm
used to rely on static variables to be initialized once and then
never again. Thus for me
static int b = a;
(which doesn't work as long a isn't a constant expression) and
static int b;
b = a;
are two rather different things. If 'a' is a constant expression
then 'b' should be set once to it the first time the function is
called and then retain any value it has at the end of the func-
tion.
Consider for example code like this you will find quite often:
void foo( ) {
static int is_uninitialized = 1;
if ( ! is_uninitialized ) {
initialize_something( );
is_uninitialized = 0;
}
...
}
Rather obviously, 'is_uninitialized' is meant to ensure that
initialize_something() is called only once, the first time the
function is called. Having it re-initialized to 1 every time
the function is called would break a lot of existing code...
But then the lccwin32 compiler allows something that isn't
defined by the standard (initialization with a non-constant
expression) and for that situation it could bend the rules
in whatever way it wants - it's on its own to define what
the further consequences are. It's not intuitive to me what
it seems to be doing but then that's of litte importantance;-)
Regards. Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
initialization of local static variable "V.Subramanian, India" <subramanian100in@yahoo.com> - 2011-10-06 23:33 -0700
Re: initialization of local static variable pete <pfiland@mindspring.com> - 2011-10-07 02:51 -0400
Re: initialization of local static variable "BartC" <bc@freeuk.com> - 2011-10-07 10:44 +0100
Re: initialization of local static variable China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-10-07 03:03 -0700
Re: initialization of local static variable "BartC" <bc@freeuk.com> - 2011-10-07 11:33 +0100
Re: initialization of local static variable jt@toerring.de (Jens Thoms Toerring) - 2011-10-07 11:11 +0000
Re: initialization of local static variable "BartC" <bc@freeuk.com> - 2011-10-07 12:51 +0100
Re: initialization of local static variable jt@toerring.de (Jens Thoms Toerring) - 2011-10-07 15:56 +0000
Re: initialization of local static variable Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-07 20:29 +0100
Re: initialization of local static variable jacob navia <jacob@spamsink.net> - 2011-10-07 21:38 +0200
Re: initialization of local static variable Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-07 21:16 +0100
Re: initialization of local static variable jacob navia <jacob@spamsink.net> - 2011-10-07 22:22 +0200
Re: initialization of local static variable jt@toerring.de (Jens Thoms Toerring) - 2011-10-07 22:25 +0000
Re: initialization of local static variable jacob navia <jacob@spamsink.net> - 2011-10-08 00:40 +0200
Re: initialization of local static variable Ian Collins <ian-news@hotmail.com> - 2011-10-08 11:56 +1300
Re: initialization of local static variable jacob navia <jacob@spamsink.net> - 2011-10-08 01:53 +0200
Re: initialization of local static variable Jens Gustedt <jens.gustedt@loria.fr> - 2011-10-09 23:04 +0200
Re: initialization of local static variable Andrew Haley <andrew29@littlepinkcloud.invalid> - 2011-10-10 11:54 -0500
Re: initialization of local static variable Ike Naar <ike@sverige.freeshell.org> - 2011-10-10 17:41 +0000
Re: initialization of local static variable Andrew Haley <andrew29@littlepinkcloud.invalid> - 2011-10-10 17:35 -0500
Re: initialization of local static variable jt@toerring.de (Jens Thoms Toerring) - 2011-10-08 00:02 +0000
Re: initialization of local static variable "BartC" <bc@freeuk.com> - 2011-10-08 01:26 +0100
Re: initialization of local static variable "Philip Lantz" <prl@canterey.us> - 2011-10-08 03:01 -0700
Re: initialization of local static variable "BartC" <bc@freeuk.com> - 2011-10-07 21:42 +0100
Re: initialization of local static variable China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-10-07 16:25 -0700
Re: initialization of local static variable jacob navia <jacob@spamsink.net> - 2011-10-08 01:51 +0200
Re: initialization of local static variable China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-10-07 18:21 -0700
Re: initialization of local static variable Nobody <nobody@nowhere.com> - 2011-10-08 15:36 +0100
Re: initialization of local static variable Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-08 22:06 +0100
Re: initialization of local static variable Nobody <nobody@nowhere.com> - 2011-10-09 05:35 +0100
Re: initialization of local static variable Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-09 09:25 +0100
Re: initialization of local static variable "io_x" <a@b.c.invalid> - 2011-10-09 11:00 +0200
Re: initialization of local static variable Barry Schwarz <schwarzb@dqel.com> - 2011-10-09 10:56 -0700
Re: initialization of local static variable James Kuyper <jameskuyper@verizon.net> - 2011-10-09 14:30 -0400
Re: initialization of local static variable Barry Schwarz <schwarzb@dqel.com> - 2011-10-09 21:43 -0700
Re: initialization of local static variable "io_x" <a@b.c.invalid> - 2011-10-10 19:08 +0200
Re: initialization of local static variable Geoff <geoff@invalid.invalid> - 2011-10-10 16:16 -0700
Re: initialization of local static variable Ike Naar <ike@iceland.freeshell.org> - 2011-10-10 23:22 +0000
Re: initialization of local static variable James Kuyper <jameskuyper@verizon.net> - 2011-10-10 20:40 -0400
Re: initialization of local static variable Barry Schwarz <schwarzb@dqel.com> - 2011-10-10 20:54 -0700
Re: initialization of local static variable "io_x" <a@b.c.invalid> - 2011-10-11 18:45 +0200
Re: initialization of local static variable "io_x" <a@b.c.invalid> - 2011-10-11 18:51 +0200
Re: initialization of local static variable Barry Schwarz <schwarzb@dqel.com> - 2011-10-11 18:52 -0700
Re: initialization of local static variable "io_x" <a@b.c.invalid> - 2011-10-12 08:49 +0200
Re: initialization of local static variable "io_x" <a@b.c.invalid> - 2011-10-11 19:48 +0200
Re: initialization of local static variable Barry Schwarz <schwarzb@dqel.com> - 2011-10-11 21:25 -0700
Re: initialization of local static variable "io_x" <a@b.c.invalid> - 2011-10-12 08:48 +0200
Re: initialization of local static variable "io_x" <a@b.c.invalid> - 2011-10-12 08:54 +0200
Re: initialization of local static variable pete <pfiland@mindspring.com> - 2011-10-12 20:55 -0400
Re: initialization of local static variable Barry Schwarz <schwarzb@dqel.com> - 2011-10-11 18:52 -0700
Re: initialization of local static variable "io_x" <a@b.c.invalid> - 2011-10-11 19:55 +0200
Re: initialization of local static variable Keith Thompson <kst-u@mib.org> - 2011-10-11 11:24 -0700
Re: initialization of local static variable Keith Thompson <kst-u@mib.org> - 2011-10-11 12:47 -0700
Re: initialization of local static variable "io_x" <a@b.c.invalid> - 2011-10-12 08:49 +0200
Re: initialization of local static variable Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 16:09 -0800
Re: initialization of local static variable China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-10-07 16:06 -0700
Re: initialization of local static variable Seebs <usenet-nospam@seebs.net> - 2011-10-12 22:25 +0000
Re: initialization of local static variable "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2011-10-13 07:09 -0700
Re: initialization of local static variable Seebs <usenet-nospam@seebs.net> - 2011-10-17 17:21 +0000
Re: initialization of local static variable Joe Wright <joewwright@comcast.net> - 2011-10-14 14:27 -0400
Re: initialization of local static variable Richard Damon <news.x.richarddamon@xoxy.net> - 2011-10-08 00:03 -0400
Re: initialization of local static variable China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-10-08 01:27 -0700
Re: initialization of local static variable Richard Damon <news.x.richarddamon@xoxy.net> - 2011-10-08 00:08 -0400
csiph-web