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 22:25 +0000 |
| Organization | Freie Universitaet Berlin |
| Message-ID | <9f9civF6foU1@mid.uni-berlin.de> (permalink) |
| References | (5 earlier) <9f8lokFi5lU1@mid.uni-berlin.de> <0.5bf60c50a20af830bcc4.20111007202925BST.87zkhcwqga.fsf@bsb.me.uk> <j6nkfh$3tt$1@speranza.aioe.org> <0.c17dea24b635577dbcc6.20111007211624BST.87hb3kwo9z.fsf@bsb.me.uk> <j6nn1h$al8$1@speranza.aioe.org> |
jacob navia <jacob@spamsink.net> wrote:
> Le 07/10/11 22:16, Ben Bacarisse a écrit :
> > jacob navia<jacob@spamsink.net> writes:
> >
> >> Le 07/10/11 21:29, Ben Bacarisse a écrit :
> [snip]
> > The programmer may simply expect the initialisation to be done "early"
> > (as it is in C++). Whenever it get done, I'd expect the function to be
> > called only once. That, for me, is the "unexpected" part.
> Yes, I agree that this handling of static is not coherent with the
> other handling of statics in other parts of the language.
> I see that you have a point here.
> >> Are there any other reasons why it should not work?
> >
> > No, you define what it means, so it is bound to work. Do you think it
> > is useful?
> I think it makes the language more coherent. See the question of the
> original poster. He was surprised that it doesn't work, and
> intuitively, you expect it to work.
I think there are different things to consider here. First of all,
not restricting the initialization of a static variable to a con-
stant expression may have some benefits (but not being a compiler
writer and thus not having to ponder all possible consequences I
might overlook a number of hairy corner cases).
The other point is having a static variable initialized more than
once. That's something I find rather disturbing - I learned to
expect a static variable to be initialized once (may it be while
loading the program or when the function it's defined in is called
for the first time seems to me to be less crucial), but having it
change its value afterwards due to the initialization again and
again is something I wouldn't have considered to be a possibility.
As far as I can see there's a double "personality" to a static
variable. One is that it can be returned from a function (the
part of 'static' I hadn't considered but Ben pointed out a few
posts up-thread). The other is the part about "it's initialized
once and then keeps its value across function invocations". That
these two meanings overlap is probably due to the way static va-
riables are typically implemented (i.e. actually somewhere as
some global memory entity, initialized at the momemt they are
set up, just not made visible to the programmer when it's de=
fined within a function).
Your approach seems to concentrate on the "I can return it from
a function", dropping the "it's initialized only once" part of
it - with, as far as I have understood BartC's posts, still ma-
king a difference depending on how it was initialized. I.e. if
it was initialized with a constant expression it behaves as a
"standard" static variable (initialized only once), otherwise it
follows some other rules.
I don't want to argue if this is wrong - you write the compiler
and thus you define what happens - but, at least to me, it does
not make things easier to understand or seems to be more cohe-
rent since I now also have to consider how a static variable is
initialized in order to figure out how it will behave.
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