static variables & their initializations

V

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
 
M

Mark A. Odell

(e-mail address removed) (vp) wrote in

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;
}
 
P

Peter Nilsson

vp said:
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 */
 
L

Lew Pitcher

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')
 
V

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
 
J

Jack Klein

(e-mail address removed) (vp) wrote in



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;
}

--
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
 
F

Flash Gordon

On Mon, 29 Dec 2003 15:37:22 GMT
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.
 
M

Mark A. Odell

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.
 
P

pete

Mark said:
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.
 
J

JV

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top