initializing variables

A

Andreas Boehm

Hi *.*,

does the standard meanwhile define something about initializing
variables by the compiler? I think, it is a side-effect of the OS used,
if undefined global (static) variables are initialized to zero.
What does the standard define for this?

thanx
Andreas
 
R

Richard Bos

Andreas Boehm said:
does the standard meanwhile define something about initializing
variables by the compiler?

Depends on their duration.
I think, it is a side-effect of the OS used,
if undefined global (static) variables are initialized to zero.
What does the standard define for this?

No, it's not OS-dependent at all; this is completely defined by the
Standard. But there are some details one must keep in mind.

First, there's a difference between what is commonly called global, and
static. Global can mean two things: external linkage (can be accessed by
all functions in the program), or file scope (can be accessed by all
functions in the file, regardless of whether it also has external
linkage). The former has no influence on initialisation; the latter has,
but only sideways.
What actually determines whether an object is automatically initialised
to zero, is whether it has static _duration_. But there are two kinds of
objects which have this: file scope objects, which by necessity also
always have static duration; and block scope objects declared with the
"static" keyword ("local statics").
Whether a file scope object is declared static has, confusingly, nothing
to do with whether it has static duration, but only with its linkage. A
file scope object declared without "static" has external linkage; one
declared with "static" is internal.
To recapitulate, take the following source file:

int a;
static int b;

void func1(void)
{
int c;
}

void func2(void)
{
static int d;
}

The only int not to be initialised to zero is c. a and b are file scope
objects, meaning that both func1() and func2() can use them. Both have
static duration, and because of this, both are initialised to zero.
However, a has external linkage: _another_ source file can declare

extern int a;

and this will refer to _this_ int a; if it declares

extern int b;

this will still not refer to this b, because that has internal linkage.
Still, you'll sometimes see people refer to both a and b as global.

Both c and d have block scope, meaning they are only visible in their
respective functions. They have no linkage. d has been declared static,
so it has static duration; it will be initialised to zero - once, at the
beginning of the program, not at every call to func2(). c, OTOH, has not
been declared static, so it has automatic duration, and it will never be
initialised. Its value is unknown (and may be a trap representation)
each time func1() is called again.

Richard
 

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

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,103
Latest member
VinaykumarnNevatia
Top