Re: Initialization is done before the program starts executing ?
- From: "Lew Pitcher" <lpitcher@xxxxxxxxxxxx>
- Date: 21 Sep 2006 08:44:34 -0700
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
lovecreatesbea...@xxxxxxxxx wrote:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the
initialization is done once only, conceptually before the program
starts executing, ... .
"Non-automatic variables are initialized before the program starts
executing." -- What does this mean?
It means that the programmer can presume that s/he does not have to
write explicit program logic to initialize non-automatic variables
What is the name of the stage in which the mentioned initialization is
performed? Compile-time or run-time?
Yes. No. Either or, or both.
For the purposes of K&R, (and likely of the C standard as well), it
doesn't matter how the compiler's architect chose to implement such
initialization. It only matters that such initialization takes place.
The architect could arrange for the initialization of non-automatic
variables to occur as part of a start-up code that executes
(conceptually) before main() is invoked.
Or, the architect could arrange for the compile and link process to
write out a loadable "image" of the memory assigned to non-automatic
variables, complete with all the blanks filled in, so that when the
system loads the program, the initialization has already occurred.
Or, the architect could arrange for a combination of the two, or even
some other mechanism to occur. In any case, from the programmers point
of view, such initialization takes place outside of and before his
logic, and is transparent to him.
In the following snippet, variables b and c are defined at line 7 & 8
but the initialization or assignment to them is not performed.
For one thing, they are not "non-automatic variables".
Think "static" or "extern" rather than "auto" or default scope.
The
declaration/definition and initialization/assignment are in the same
scope, why the declaration is executed but the initialization is not?
P'haps one of our resident experts can answer this better. Think of the
statement
int b = 30;
as doing three separate things.
It
1) tells the compiler that, when the programmer references the variable
"b", treat such references as accesses to an integer value stored as an
automatic variable,
2) generates the code to allocate the space for that automatic
variable, and
3) generates the code to initialize the automatic variable.
It is likely that any optimization that the compiler does for automatic
variables "rolls up" all variables (within a particular level of scope,
in this case bounded by the edges of the switch() compound statement)
into one big allocation, which happens at the entry to the scope.
However, it is less likely that the compiler can perform the same sort
of "roll up" operation on initializations, and it might leave them as
hidden operations to be performed in sequence after the entry to the
scope level. Now, since your switch() function branches to specific
entrypoints in the compound statement, these initializations are
possibly bypassed.
#include <stdio.h>
int main(void){
int a = 1;
a is an automatic variable
switch(a){
int b = 30; /*line 7*/
b is an automatic variable
int c; /*line 8*/
c is an automatic variable
c = 20; /*line 9*/
case 1: /*fall-down*/
default:
printf("b: %d, c: %d\n", b, c);
}
return 0;
}
/*Result:
b: 4598440, c: 4198571*/
HTH
- --
Lew Pitcher
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12
iD8DBQFFErNaagVFX4UWr64RAgVtAKCm8ry42nli/k4ISnFT3qY7Hbk3cACfdu32
kUeNYIkk1LnXXnGQT1NzZUs=
=y5JV
-----END PGP SIGNATURE-----
.
- Follow-Ups:
- Re: Initialization is done before the program starts executing ?
- From: lovecreatesbea...@xxxxxxxxx
- Re: Initialization is done before the program starts executing ?
- References:
- Initialization is done before the program starts executing ?
- From: lovecreatesbea...@xxxxxxxxx
- Initialization is done before the program starts executing ?
- Prev by Date: Re: scaling coefficients for c
- Next by Date: Re: strange getch+kbhit behaviour
- Previous by thread: Re: Initialization is done before the program starts executing ?
- Next by thread: Re: Initialization is done before the program starts executing ?
- Index(es):
Relevant Pages
|