470,572 Members | 2,478 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,572 developers. It's quick & easy.

Initialization of Static variables

Why the static variable in a function should be initialized by a
constant? Why not any normal variable.
Dec 21 '07 #1
4 1957
On Dec 20, 11:37*pm, riva <ra.ravi....@gmail.comwrote:
Why the static variable in a function should be initialized by a
constant? Why not any normal variable.
Static variables are initialized to NULL or zero as appropriate
automatically.
If you want to set it to a specific value other than zero, then you
should do that. Of course, that is also true for automatic variables.
Dec 21 '07 #2
riva said:
Why the static variable in a function should be initialized by a
constant? Why not any normal variable.
2.1.2 Execution environments

"[...] All objects in static storage shall be initialized (set to their
initial values) before program startup."

Before program startup, the value of a "normal variable" (as you call it)
is not known, so clearly it cannot be used for initialising a static
object's value.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Dec 21 '07 #3
riva wrote:
Why the static variable in a function should be initialized by a
constant? Why not any normal variable.
Static variables are initialised /once/, before the program
proper starts executing. There are no "normal" (I'm taking
you to mean "automatic") variables to be had.

If you want to set your static to a dynamic value, you
can use a common-or-garden assignment statement.

Since one of the points of a static variable is that it
retains its value between calls of its enclosing function,
having it re-initialised on each call would be ... unhelpful.

--
Dynamic Not Duo Hedgehog
"Who do you serve, and who do you trust?" /Crusade/

Dec 21 '07 #4
riva wrote:
>
Why the static variable in a function should be initialized by a
constant? Why not any normal variable.
Because the static variable is not 'initialized' by code. The
value is set in an image of the static structures, and loaded from
that. That initialization method can't reach the 'normal variable'
value.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Dec 21 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by lovecreatesbea... | last post: by
8 posts views Thread by Per Bull Holmen | last post: by
6 posts views Thread by Christof Warlich | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.