Initialization is done before the program starts executing ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lovecreatesbea...@gmail.com

    #1

    Initialization is done before the program starts executing ?

    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? What is the name of the stage in
    which the mentioned initialization is performed? Compile-time or
    run-time?

    In the following snippet, variables b and c are defined at line 7 & 8
    but the initialization or assignment to them is not performed. The
    declaration/definition and initialization/assignment are in the same
    scope, why the declaration is executed but the initialization is not?

    #include <stdio.h>

    int main(void){
    int a = 1;

    switch(a){
    int b = 30; /*line 7*/
    int c; /*line 8*/
    c = 20; /*line 9*/

    case 1: /*fall-down*/
    default:
    printf("b: %d, c: %d\n", b, c);
    }

    return 0;
    }

    /*Result:
    b: 4598440, c: 4198571*/

  • A. Bolmarcich

    #2
    Re: Initialization is done before the program starts executing ?

    On 2006-09-21, lovecreatesbea. ..@gmail.com <lovecreatesbea uty@gmail.comwr ote:
    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? What is the name of the stage in
    which the mentioned initialization is performed? Compile-time or
    run-time?
    It means that each non-automatic variable is set to its initial value
    before the function of the C program that is called at program
    starup is called. The name of the function called at program startup
    is usually "main". The initialization can be preformed any time
    before the first statement of function called at program startup
    executes. Different compilers and execution environments may do the
    initiailzation at different times.
    In the following snippet, variables b and c are defined at line 7 & 8
    but the initialization or assignment to them is not performed. The
    declaration/definition and initialization/assignment are in the same
    scope, why the declaration is executed but the initialization is not?
    Because the variables declared/defined at lines 7 & 8 are automatic
    variables, the above quote from K&R2 does not apply to them. For
    more information, please see my reply (message-id
    <slrneh2nsf.2k4 s.aggedor@earl-grey.cloud9.net >) in the "why does C
    standard allow this declaration" thread of this newsgroup.
    #include <stdio.h>
    >
    int main(void){
    int a = 1;
    >
    switch(a){
    int b = 30; /*line 7*/
    int c; /*line 8*/
    c = 20; /*line 9*/
    >
    case 1: /*fall-down*/
    default:
    printf("b: %d, c: %d\n", b, c);
    }
    >
    return 0;
    }
    >
    /*Result:
    b: 4598440, c: 4198571*/
    >

    Comment

    • Lew Pitcher

      #3
      Re: Initialization is done before the program starts executing ?

      -----BEGIN PGP SIGNED MESSAGE-----
      Hash: SHA1


      lovecreatesbea. ..@gmail.com 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

      iD8DBQFFErNaagV FX4UWr64RAgVtAK Cm8ry42nli/k4ISnFT3qY7Hbk3 cACfdu32
      kUeNYIkk1LnXXnG QT1NzZUs=
      =y5JV
      -----END PGP SIGNATURE-----

      Comment

      • lovecreatesbea...@gmail.com

        #4
        Re: Initialization is done before the program starts executing ?


        A. Bolmarcich wrote:
        On 2006-09-21, lovecreatesbea. ..@gmail.com <lovecreatesbea uty@gmail.comwr ote:
        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? What is the name of the stage in
        which the mentioned initialization is performed? Compile-time or
        run-time?
        >
        It means that each non-automatic variable is set to its initial value
        before the function of the C program that is called at program
        starup is called. The name of the function called at program startup
        is usually "main". The initialization can be preformed any time
        before the first statement of function called at program startup
        executes. Different compilers and execution environments may do the
        initiailzation at different times.
        If the main is not called and the program never even starts, how can
        the non-automatic variables are defined and initialized?
        In the following snippet, variables b and c are defined at line 7 & 8
        but the initialization or assignment to them is not performed. The
        declaration/definition and initialization/assignment are in the same
        scope, why the declaration is executed but the initialization is not?
        >
        Because the variables declared/defined at lines 7 & 8 are automatic
        variables, the above quote from K&R2 does not apply to them. For
        more information, please see my reply (message-id
        <slrneh2nsf.2k4 s.aggedor@earl-grey.cloud9.net >) in the "why does C
        standard allow this declaration" thread of this newsgroup.
        >
        #include <stdio.h>

        int main(void){
        int a = 1;

        switch(a){
        int b = 30; /*line 7*/
        int c; /*line 8*/
        c = 20; /*line 9*/

        case 1: /*fall-down*/
        default:
        printf("b: %d, c: %d\n", b, c);
        }

        return 0;
        }

        /*Result:
        b: 4598440, c: 4198571*/
        You mean the initialization and assignment at line 7 & 9 are
        unreachable, but why the declaration and definition at the same line 7
        and line 8 are reached?

        Comment

        • lovecreatesbea...@gmail.com

          #5
          Re: Initialization is done before the program starts executing ?


          Lew Pitcher wrote:
          lovecreatesbea. ..@gmail.com 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.
          Thank you. Your explanation is helpful to me.
          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.
          Yes, I just asked another question :)
          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*/

          Comment

          • A. Bolmarcich

            #6
            Re: Initialization is done before the program starts executing ?

            On 2006-09-21, lovecreatesbea. ..@gmail.com <lovecreatesbea uty@gmail.comwr ote:
            >
            A. Bolmarcich wrote:
            >It means that each non-automatic variable is set to its initial value
            >before the function of the C program that is called at program
            >starup is called. The name of the function called at program startup
            >is usually "main". The initialization can be preformed any time
            >before the first statement of function called at program startup
            >executes. Different compilers and execution environments may do the
            >initiailzati on at different times.
            >
            If the main is not called and the program never even starts, how can
            the non-automatic variables are defined and initialized?
            If the program never even starts, it does not matter whether
            non-automated been initialized.
            >Because the variables declared/defined at lines 7 & 8 are automatic
            >variables, the above quote from K&R2 does not apply to them. For
            >more information, please see my reply (message-id
            ><slrneh2nsf.2k 4s.aggedor@earl-grey.cloud9.net >) in the "why does C
            >standard allow this declaration" thread of this newsgroup.
            >>
            #include <stdio.h>
            >
            int main(void){
            int a = 1;
            >
            switch(a){
            int b = 30; /*line 7*/
            int c; /*line 8*/
            c = 20; /*line 9*/
            >
            case 1: /*fall-down*/
            default:
            printf("b: %d, c: %d\n", b, c);
            }
            >
            return 0;
            }
            >
            /*Result:
            b: 4598440, c: 4198571*/
            >
            >
            You mean the initialization and assignment at line 7 & 9 are
            unreachable, but why the declaration and definition at the same line 7
            and line 8 are reached?
            Declarations are not executable statements; the initialzation of an
            automatic variable is like an executable assignment expression. Please
            read the reply in the other thread that I mentioned.

            Comment

            • Jack Klein

              #7
              Re: Initialization is done before the program starts executing ?

              On 21 Sep 2006 08:04:06 -0700, "lovecreatesbea ...@gmail.com"
              <lovecreatesbea uty@gmail.comwr ote in comp.lang.c:
              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? What is the name of the stage in
              which the mentioned initialization is performed? Compile-time or
              run-time?
              >
              In the following snippet, variables b and c are defined at line 7 & 8
              but the initialization or assignment to them is not performed. The
              declaration/definition and initialization/assignment are in the same
              scope, why the declaration is executed but the initialization is not?
              >
              #include <stdio.h>
              >
              int main(void){
              int a = 1;
              >
              switch(a){
              int b = 30; /*line 7*/
              int c; /*line 8*/
              c = 20; /*line 9*/
              >
              case 1: /*fall-down*/
              default:
              printf("b: %d, c: %d\n", b, c);
              }
              >
              return 0;
              }
              >
              /*Result:
              b: 4598440, c: 4198571*/
              Yesterday, in another thread about the same issue, I used the term
              "flow of control".

              I think you are slightly confused about two different concepts and
              mixing them together.

              These two concepts are scope and execution flow.

              Scope is a compile time concept. A compiler processes a translation
              unit in strict linear order (or at least must produce results as if it
              did), from the first line of the source file through the last. A
              simple description of the C standard "translatio n unit" is a source
              file and anything it includes. Any headers or included files are
              processed in linear order from their first line to their last, and
              then processing returns to the next line in the file that included
              them.

              Every identifier in translation unit has a scope, from the point of
              the identifier's declaration to the end of the scope it was declared
              in. If it is declared at file scope in a translation unit, whether in
              the source file or in something included, it is in scope from that
              line through the last line in the source file. If an identifier is
              defined inside any block, it has scope from the point of its
              declaration until the end of that block.

              When a C program is executed, it is actually very rare for the flow of
              execution to be strictly in order from top to bottom. Flow control
              statements like if, else, while, for, goto, and switch cause
              statements to be skipped or looped. But this is a run time matter,
              and has no effect on scope, which was a compile time matter.

              So 'b' has a scope that starts on the line where it is declared (a
              definition is also a declaration) and initialized. This scope ends
              seven lines later on the line containing the closing brace of the
              included switch statement.

              C makes a distinction between initialization and an assignment. An
              assignment is always a statement, a declaration with an initializer is
              not a statement. But for an automatic variable, an initializer is
              "like" a statement, in that it only actually happens if the flow of
              control passes through it.

              It can be useful to imagine something that you could call "an
              execution pointer", that points to each statement as it is executed.
              If the "execution pointer" never points to the declarator that
              initializes an auto object, that initialization is not performed.

              But this has no effect on the scope of the object's identifier, which
              has nothing at all to do with when, or if, the "execution pointer"
              ever touches the identifier's declaration.

              --
              Jack Klein
              Home: http://JK-Technology.Com
              FAQs for
              comp.lang.c http://c-faq.com/
              comp.lang.c++ http://www.parashift.com/c++-faq-lite/
              alt.comp.lang.l earn.c-c++

              Comment

              • lovecreatesbea...@gmail.com

                #8
                Re: Initialization is done before the program starts executing ?


                Jack Klein wrote:
                On 21 Sep 2006 08:04:06 -0700, "lovecreatesbea ...@gmail.com"
                <lovecreatesbea uty@gmail.comwr ote in comp.lang.c:
                >
                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? What is the name of the stage in
                which the mentioned initialization is performed? Compile-time or
                run-time?

                In the following snippet, variables b and c are defined at line 7 & 8
                but the initialization or assignment to them is not performed. The
                declaration/definition and initialization/assignment are in the same
                scope, why the declaration is executed but the initialization is not?

                #include <stdio.h>

                int main(void){
                int a = 1;

                switch(a){
                int b = 30; /*line 7*/
                int c; /*line 8*/
                c = 20; /*line 9*/

                case 1: /*fall-down*/
                default:
                printf("b: %d, c: %d\n", b, c);
                }

                return 0;
                }

                /*Result:
                b: 4598440, c: 4198571*/
                <snip>
                It can be useful to imagine something that you could call "an
                execution pointer", that points to each statement as it is executed.
                If the "execution pointer" never points to the declarator that
                initializes an auto object, that initialization is not performed.
                >
                But this has no effect on the scope of the object's identifier, which
                has nothing at all to do with when, or if, the "execution pointer"
                ever touches the identifier's declaration.
                Thank you.

                Why can the initialization/assignment at line 7 & 9 above be omitted
                but their declaration can't? When is a variable declared/defined? It
                should not be declared when you write down that line of code in the
                source editor, or when the compiler is invoked.

                Comment

                • mark_bluemel@pobox.com

                  #9
                  Re: Initialization is done before the program starts executing ?


                  lovecreatesbea. ..@gmail.com wrote:
                  #include <stdio.h>
                  >
                  int main(void){
                  int a = 1;
                  >
                  switch(a){
                  int b = 30; /*line 7*/
                  int c; /*line 8*/
                  c = 20; /*line 9*/
                  >
                  case 1: /*fall-down*/
                  default:
                  printf("b: %d, c: %d\n", b, c);
                  }
                  >
                  return 0;
                  }
                  >
                  /*Result:
                  b: 4598440, c: 4198571*/
                  Why can the initialization/assignment at line 7 & 9 above be omitted
                  but their declaration can't? When is a variable declared/defined? It
                  should not be declared when you write down that line of code in the
                  source editor, or when the compiler is invoked.
                  Why not? What source do you have to validate that assertion?

                  I think you are in danger of defining your own C standard and
                  complaining when the real C standard doesn't match your expectations.
                  Both Lew and Jack have given detailed explanations, and Keith
                  Thompson's discussion in another thread you started on the same issue
                  gives a very clear explanation.

                  Accept that that's the way the language works and move on.

                  Comment

                  Working...