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.
--
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
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.
>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.
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().
--
Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
>
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++.
On Sun, 16 Mar 2008 09:50:55 +1300, Ian Collins wrote:
Richard Heathfield wrote:
>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++.
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.
>>
>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++.
I think a freestanding program might have difficulty in initialising
static variables with function calls.
Comment