static variable and function

A

asit

Static variables can't be initialized with functions, becoz these are
load time processes. Why this is so ??

What is a load time process ??
 
R

Richard Heathfield

asit said:
Static variables can't be initialized with functions, becoz these are
load time processes. Why this is so ??

Static objects can only be initialised with constant expressions. "All the
expressions in an initializer for an object that has static storage
duration or in an initializer list for an object that has aggregate or
union type shall be constant expressions", says my C89 draft. "All the
expressions in an initializer for an object that has static storage
duration shall be constant expressions or string literals", says my C99
final.

Since the result of a function call can be neither a string literal
(although it might be a pointer to the first character therein) nor a
constant expression, it can't be used to initialise a static object.

Furthermore, C99 goes on to say that "All objects with static storage
duration shall be initialized (set to their initial values) before program
startup" (and C89 has very similar wording).

Since functions can't be called until the program has started, they can't
return values until the program has started - by which time all static
objects have already been initialised, so it's a bit late by then, right?
What is a load time process ??

Heaven knows.
 
S

santosh

asit said:
Static variables can't be initialized with functions, becoz these are
load time processes. Why this is so ??

What is a load time process ??

Static objects are conceptually initialised just before execution
begins, therefore their initialisation expression needs to be a
constant. The first function that is called is main which takes place
after this period.

The phrase "load time process" is not defined in C. Presumably it refers
to various activities that take place when a program is loaded into
memory, and prior to it's execution. In C some such processes are
initialisations of static objects, opening the predefined streams
stdin, stdout and stderr, setting the default locale, setting up the
arguments, if any, to main etc.
 
R

rpgfan3233

Static variables can't be initialized with functions, becoz these are
load time processes. Why this is so ??

What is a load time process ??

My guess is that it is to prevent things like the following nonworking
code:

int foo (void);

int x = foo(); /* a global variable, but static variables act
similarly to global variables anyway */

int main (void)
{ printf("%d\n", x); return 0; }

int foo (void)
{ return x; } /* how can we return 'x' if 'x' is the one being
initialized? */


It is similar to a circular dependency (e.g. 'struct x' has a 'struct
y' member, and 'struct y' has a 'struct x' member), except it is a
problem with the same object instead of multiple objects.

I assume the same is true of variables declared using the 'static'
keyword since they are like global variables within a specific scope.
 
S

santosh

rpgfan3233 said:
My guess is that it is to prevent things like the following nonworking
code:

int foo (void);

int x = foo(); /* a global variable, but static variables act
similarly to global variables anyway */

In C a "global" variable is always a static variable, though not all
static variables are what you might term global.

int main (void)
{ printf("%d\n", x); return 0; }

int foo (void)
{ return x; } /* how can we return 'x' if 'x' is the one being
initialized? */


It is similar to a circular dependency (e.g. 'struct x' has a 'struct
y' member, and 'struct y' has a 'struct x' member), except it is a
problem with the same object instead of multiple objects.
I assume the same is true of variables declared using the 'static'
keyword since they are like global variables within a specific scope.

This is probably not the biggest reason for the restriction in question.
 
K

Keith Thompson

santosh said:
Static objects are conceptually initialised just before execution
begins, therefore their initialisation expression needs to be a
constant. The first function that is called is main which takes place
after this period.

The phrase "load time process" is not defined in C.
Right.

Presumably it refers
to various activities that take place when a program is loaded into
memory, and prior to it's execution. In C some such processes are
initialisations of static objects, opening the predefined streams
stdin, stdout and stderr, setting the default locale, setting up the
arguments, if any, to main etc.

In other words, everything that happens after you ask the system to
run the program and before execution reaches the opening '{' in
main().
 
I

Ian Collins

Richard said:
Since functions can't be called until the program has started, they can't
return values until the program has started - by which time all static
objects have already been initialised, so it's a bit late by then, right?
Any idea why C has this restriction? One of the (many) differences
between C and C++ is static variables can be initialised by a function
call in C++.
 
H

Harald van Dijk

Any idea why C has this restriction? One of the (many) differences
between C and C++ is static variables can be initialised by a function
call in C++.

Other differences between C and C++ already require the implementation
support execution of user code before the initial call to main, so
dynamic initialisation of static variables takes less extra effort in C++
than in C.
 
S

santosh

Ian said:
Any idea why C has this restriction? One of the (many) differences
between C and C++ is static variables can be initialised by a function
call in C++.

I think a freestanding program might have difficulty in initialising
static variables with function calls.
 

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
474,266
Messages
2,571,075
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top