By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

static variables & their initializations

P: n/a
vp
Can I safely assume that all static variables are initialized as NULL
or zero, depending on the types of the variables, no matter on which
platform that app is compiled ?

Thanks for your help,
DT
Nov 14 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
dt*******@yahoo.com (vp) wrote in
news:24**************************@posting.google.c om:
Can I safely assume that all static variables are initialized as NULL
or zero, depending on the types of the variables, no matter on which
platform that app is compiled ?


If the platform has a conforming C compiler all static *non-initialized*
variables will be zero. Same for file scoped vars.

e.g.

int foo; /* will be zero at run-time */
int fob = 2; /* won't be zero */

int main(void)
{
static int baz; /* will be zero at run-time */
static int bar = 5; /* won't be zero */
return 0;
}

--
- Mark ->
--
Nov 14 '05 #2

P: n/a
"vp" <dt*******@yahoo.com> wrote in message
news:24**************************@posting.google.c om...
Can I safely assume that all static variables are initialized as NULL
or zero, depending on the types of the variables, no matter on which
platform that app is compiled ?


Almost. You can only assume that static variables without explicit
initialisors are so initialised.

e.g. static int x = 4; will not initialise x to zero.

Sounds obvious, but a more complicated example might be...

static int x;

int f(void)
{
return x;
}

static int x = 4;

Here, x will be initialised to 4 during program startup.

Note that aggregates without complete initialisers are default
initialised...

int array[4] = { 42 }; /* array 1..3 will be 0 */

--
Peter
Nov 14 '05 #3

P: n/a
On 29 Dec 2003 06:54:03 -0800, dt*******@yahoo.com (vp) wrote:
Can I safely assume that all static variables are initialized as NULL
or zero, depending on the types of the variables, no matter on which
platform that app is compiled ?
IIRC, the final C99 standard didn't change from the draft, and the draft says:

If an object that has static storage duration is not initialized explicitly,
then:
if it has pointer type, it is initialized to a null pointer;
if it has arithmetic type, it is initialized to (positive or unsigned) zero;
if it is an aggregate, every member is initialized (recursively) according
to these rules;
if it is a union, the first named member is initialized (recursively)
according to these rules.

If a compiler doesn't follow these rules, then it is in error. That's not to say
that all compilers follow the rules, though.
Thanks for your help,
DT


--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
Nov 14 '05 #4

P: n/a
vp
Mark, Peter, Lew, thanks for all of your quick replies.

Yes, I am interested in the static variables that are not explicitly
initialized by the program.

Your replies let me know exactly what I want to be sure.

Thanks again,

DT
Nov 14 '05 #5

P: n/a
On 29 Dec 2003 15:20:12 GMT, "Mark A. Odell" <no****@embeddedfw.com>
wrote in comp.lang.c:
dt*******@yahoo.com (vp) wrote in
news:24**************************@posting.google.c om:
Can I safely assume that all static variables are initialized as NULL
or zero, depending on the types of the variables, no matter on which
platform that app is compiled ?
If the platform has a conforming C compiler all static *non-initialized*
variables will be zero. Same for file scoped vars.


Terminology problem here, all file scope variables have static storage
duration regardless of the use of the overloaded static keyword.
e.g.

int foo; /* will be zero at run-time */
This is a static int object. It has external linkage, whereas adding
the static keyword to the definition would change the linkage to
internal. But it is static (has static storage duration) either way.
int fob = 2; /* won't be zero */

int main(void)
{
static int baz; /* will be zero at run-time */
static int bar = 5; /* won't be zero */
return 0;
}

--
- Mark ->


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 14 '05 #6

P: n/a
Jack Klein <ja*******@spamcop.net> wrote in
news:0m********************************@4ax.com:
> Can I safely assume that all static variables are initialized as NULL
> or zero, depending on the types of the variables, no matter on which
> platform that app is compiled ?


If the platform has a conforming C compiler all static
*non-initialized* variables will be zero. Same for file scoped vars.


Terminology problem here, all file scope variables have static storage
duration regardless of the use of the overloaded static keyword.


True enough. In fact I just think of 'static' as always meaning reduced
scope and of static duration. Even though static on block scoped vars.
doesn't really reduce the scope of said var, it is a harmless lie for me.

--
- Mark ->
--
Nov 14 '05 #7

P: n/a
Mark A. Odell wrote:

Jack Klein <ja*******@spamcop.net> wrote in
news:0m********************************@4ax.com:
> Can I safely assume that all
> static variables are initialized as NULL
> or zero, depending on the types of the variables,
> no matter on which platform that app is compiled ?

If the platform has a conforming C compiler all static
*non-initialized* variables will be zero.
Same for file scoped vars.


Terminology problem here,
all file scope variables have static storage
duration regardless of the use of the overloaded static keyword.


True enough.
In fact I just think of 'static' as always meaning reduced
scope and of static duration. Even though static on block scoped vars.
doesn't really reduce the scope of said var,
it is a harmless lie for me.


I think of static locals,
as globals with reduced scope and internal linkage.
(global == external complete object)
The common points being static duration, and default initialization.

--
pete
Nov 14 '05 #8

P: n/a
On Mon, 29 Dec 2003 15:37:22 GMT
Le*********@td.com (Lew Pitcher) wrote:
On 29 Dec 2003 06:54:03 -0800, dt*******@yahoo.com (vp) wrote:
Can I safely assume that all static variables are initialized as NULL
or zero, depending on the types of the variables, no matter on which
platform that app is compiled ?


IIRC, the final C99 standard didn't change from the draft, and the
draft says:

If an object that has static storage duration is not initialized
explicitly, then:
_ if it has pointer type, it is initialized to a null pointer;
_ if it has arithmetic type, it is initialized to (positive or
unsigned) zero;_ if it is an aggregate, every member is initialized
(recursively) according
to these rules;
_ if it is a union, the first named member is initialized
(recursively)
according to these rules.

If a compiler doesn't follow these rules, then it is in error. That's
not to say that all compilers follow the rules, though.


IIRC (and I could be wrong) *one* C compiler I used for embedded work
was broken in that it only initialised static variables that were
explicitly initialised in the source code. However, this was easily
fixed by modifying the bootstrap code (the source of which was provided)
to initialise all RAM to zero (null pointers being all bits 0 in this
case) before doing other initialisation. This was back in 1995, so I
don't know if non-conforming compilers are still around.
--
Flash Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spam, it is real and I read it.
Nov 14 '05 #9

P: n/a
JV

"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:20031230125718.5a79b5c4.sp**@flash-gordon.me.uk...
IIRC (and I could be wrong) *one* C compiler I used for embedded work
was broken in that it only initialised static variables that were
explicitly initialised in the source code. However, this was easily
fixed by modifying the bootstrap code (the source of which was provided)
to initialise all RAM to zero (null pointers being all bits 0 in this
case) before doing other initialisation. This was back in 1995, so I
don't know if non-conforming compilers are still around.
--
Flash Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spam, it is real and I read it.


I've seen a similiar trouble in one compiler and I also work with embedded
systems.So it is not so common, but if can been pretty hard to figure out
when it happens, so I would still allways intialize the static variables if
it is required that they have a certain value when program starts.
-Jyrki
Nov 14 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.