Re: Initialization is done before the program starts executing ?



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

.



Relevant Pages

  • Re: save attribute for module variables
    ... With some other compiler ... | variable comes into scope after being out of scope, ... | initialization should be applied. ... Since the standard doesn't ...
    (comp.lang.fortran)
  • Re: save attribute for module variables
    ... >> | variable comes into scope after being out of scope, ... >> | initialization should be applied. ... Since the standard doesn't ... > and then examining the value, tell whether your compiler implemented the ...
    (comp.lang.fortran)
  • Re: save attribute for module variables
    ... || it required that the compiler should apply the default initialisation ... | initialized module variables - as indeed, if I'm not mistaken, all initialized ...
    (comp.lang.fortran)
  • Re: save attribute for module variables
    ... In that event, every time the | variable comes into scope after being out of scope, the default ... | initialization should be applied. ... with CVF6.6C and presence of SAVE made no difference (nor the compiler complained about it, ... As the standard did not want this implementation difference to become visible - this is what I believe is called a post hoc rationalization - the standard says that initialized module variables - as indeed, if I'm not mistaken, all initialized variables - automatically acquire the SAVE attribute. ...
    (comp.lang.fortran)
  • Re: Initialization is done before the program starts executing ?
    ... initialization is done once only, ... starts executing, ... ... "Non-automatic variables are initialized before the program starts ... standard allow this declaration" thread of this newsgroup. ...
    (comp.lang.c)