Object Initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sarathy

    #1

    Object Initialization

    Hi,
    I read the following points in K&R "Section A8.7 Initialization" .
    Seems like i have a problem with them.

    * All expressions in the initialization of constant object/array must
    be constant expression. [OK,fine]

    * Expressions in the initializer for an auto or register object or
    array must likewise be constant expressions if the initializer is a
    brace enclosed list. [ ????]

    But

    auto int a=10;
    auto int b[]={a,a++};
    works fine.

    * If the initializer for an automatic object[auto or register] is a
    single expression, it need not be a constant expression.

    What does single expression mean here ?

    Regards,
    Sarathy

  • Richard Heathfield

    #2
    Re: Object Initialization

    sarathy said:

    <snip>
    auto int b[]={a,a++};
    works fine.
    Someone told me that, in basketball, you have to bounce the
    ball off the floor; you can't hold the ball and run around the
    court. But I tried it and it works just fine. Obviously he
    does not understand basketball!
    -- Attr. Steve Summit

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at above domain (but drop the www, obviously)

    Comment

    • jaysome

      #3
      Re: Object Initialization

      On 22 Jul 2006 01:14:38 -0700, "sarathy" <sps.sarathy@gm ail.com>
      wrote:
      >Hi,
      I read the following points in K&R "Section A8.7 Initialization" .
      >Seems like i have a problem with them.
      >
      >* All expressions in the initialization of constant object/array must
      >be constant expression. [OK,fine]
      >
      >* Expressions in the initializer for an auto or register object or
      >array must likewise be constant expressions if the initializer is a
      >brace enclosed list. [ ????]
      >
      >But
      >
      auto int a=10;
      auto int b[]={a,a++};
      works fine.
      >
      >* If the initializer for an automatic object[auto or register] is a
      >single expression, it need not be a constant expression.
      >
      What does single expression mean here ?
      That's a really good question. As far as I can tell, the C Standard
      never really explains that. Not even the Appendix that lists all the
      types of expressions mentions the term "single expression". Maybe it's
      the same as a "primary expression"? Whatever it is, it certainly isn't
      obvious.

      --
      jay

      Comment

      • Keith Thompson

        #4
        Re: Object Initialization

        "sarathy" <sps.sarathy@gm ail.comwrites:
        I read the following points in K&R "Section A8.7 Initialization" .
        Seems like i have a problem with them.
        >
        * All expressions in the initialization of constant object/array must
        be constant expression. [OK,fine]
        >
        * Expressions in the initializer for an auto or register object or
        array must likewise be constant expressions if the initializer is a
        brace enclosed list. [ ????]
        Right. (I *think* that restriction was lifted in C99; at least, I
        can't find it in C99 6.7.8, and gcc complains about it with
        "-ansi -pedantic" but not with "-std=c99 -pedantic". My copy of the
        C90 standard isn't handy at the moment.)
        But
        >
        auto int a=10;
        auto int b[]={a,a++};
        works fine.
        That doesn't mean it's valid. Possibly your compiler supports it as
        an extension; it would probably complain in conforming mode. (If
        you're using gcc, see above.)
        * If the initializer for an automatic object[auto or register] is a
        single expression, it need not be a constant expression.
        >
        What does single expression mean here ?
        A single expression is simply an expression. For example, in the
        example above, 10 is a (single) expression, but {a,a++} is not an
        expression; it's two expressions separated by a comma and enclosed in
        braces.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
        We must do something. This is something. Therefore, we must do this.

        Comment

        • Keith Thompson

          #5
          Re: Object Initialization

          jaysome <jaysome@spamco p.netwrites:
          On 22 Jul 2006 01:14:38 -0700, "sarathy" <sps.sarathy@gm ail.com>
          wrote:
          [...]
          >>* If the initializer for an automatic object[auto or register] is a
          >>single expression, it need not be a constant expression.
          >>
          > What does single expression mean here ?
          >
          That's a really good question. As far as I can tell, the C Standard
          never really explains that. Not even the Appendix that lists all the
          types of expressions mentions the term "single expression". Maybe it's
          the same as a "primary expression"? Whatever it is, it certainly isn't
          obvious.
          It becomes obvious as soon as you stop making it complicated. 8-)}

          A "single expression" isn't some special kind of expression; it's just
          an expression, with "single" being used in its ordinary English sense
          (i.e., there's just one).

          An initializer can either be a (single) expression or a list of
          expressions separated by commas; the latter is not itself an
          expression.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • Chris Torek

            #6
            Re: Object Initialization

            >"sarathy" <sps.sarathy@gm ail.comwrites:
            >* Expressions in the initializer for an auto or register object or
            >array must likewise be constant expressions if the initializer is a
            >brace enclosed list. [ ????]
            In article <lnwta6t3d1.fsf @nuthaus.mib.or g>,
            Keith Thompson <kst-u@mib.orgwrote:
            >Right. (I *think* that restriction was lifted in C99; at least, I
            >can't find it in C99 6.7.8, and gcc complains about it with
            >"-ansi -pedantic" but not with "-std=c99 -pedantic". My copy of the
            >C90 standard isn't handy at the moment.)
            It was indeed lifted in C99.

            The restriction in C89 makes it possible for compilers to "pretend"
            that, in:

            void f(void) {
            int arr[5] = { 1, 2, 3 };
            ...
            }

            you wrote:

            void f(void) {
            static int arr_init[5] = { 1, 2, 3 };
            int arr[5];
            memcpy(arr, arr_init, sizeof arr);
            ...
            }

            Without the restriction, the compiler actually has to work hard
            sometimes (gosh, imagine that :-) ). But it always seemed a little
            silly to me, since C89 does allow things like:

            void f(void) {
            int v1 = call_some_func( );
            int v2[5] = { 1, 2, 3 };
            int v3 = call_another_fu nc();
            char v4[] = "text";
            ...
            }

            which, if you are doing any kind of optimization at all in your
            compiler, has to "save up" the initialization of v1 and v3 until
            you have decided how much space to allocate for the activation
            record for f() (since, in general, one should not call functions
            until the activation record has been established). So being able
            to stash the initializers for v2 and v4 in some alternative text
            space, then memcpy() them into place, merely shortens the four
            deferred initializations slightly -- the compiler could, as it
            must for C99, take:

            void f(void) {
            int v1 = call_some_func( );
            int v2[5] = { v1, 0, v1, 0, v1 };
            int v3 = call_another_fu nc();
            ...
            }

            and generate code "as if" you wrote:

            void f(void) {
            int v2[5], v3, v1;
            v1 = call_some_func( );
            v2[0] = v2[2] = v2[4] = v1;
            v2[1] = v2[3] = 0;
            v3 = call_another_fu nc();
            ...
            }

            It really is not that hard. :-)
            --
            In-Real-Life: Chris Torek, Wind River Systems
            Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
            email: forget about it http://web.torek.net/torek/index.html
            Reading email is like searching for food in the garbage, thanks to spammers.

            Comment

            • sarathy

              #7
              Re: Object Initialization

              Hi,
              The initialization

              int c=2,d=4;
              int a[]={c,d};

              gave "Illegal Initialization" error when compiled with Turbo C
              But the error was supressed in gcc.

              Thanks all.
              Regards,
              Sarathy


              Keith Thompson wrote:
              "sarathy" <sps.sarathy@gm ail.comwrites:
              I read the following points in K&R "Section A8.7 Initialization" .
              Seems like i have a problem with them.

              * All expressions in the initialization of constant object/array must
              be constant expression. [OK,fine]

              * Expressions in the initializer for an auto or register object or
              array must likewise be constant expressions if the initializer is a
              brace enclosed list. [ ????]
              >
              Right. (I *think* that restriction was lifted in C99; at least, I
              can't find it in C99 6.7.8, and gcc complains about it with
              "-ansi -pedantic" but not with "-std=c99 -pedantic". My copy of the
              C90 standard isn't handy at the moment.)
              >
              But

              auto int a=10;
              auto int b[]={a,a++};
              works fine.
              >
              That doesn't mean it's valid. Possibly your compiler supports it as
              an extension; it would probably complain in conforming mode. (If
              you're using gcc, see above.)
              >
              * If the initializer for an automatic object[auto or register] is a
              single expression, it need not be a constant expression.

              What does single expression mean here ?
              >
              A single expression is simply an expression. For example, in the
              example above, 10 is a (single) expression, but {a,a++} is not an
              expression; it's two expressions separated by a comma and enclosed in
              braces.
              >
              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              • Coos Haak

                #8
                Re: Object Initialization

                Op 22 Jul 2006 12:58:33 -0700 schreef sarathy:
                Hi,
                The initialization
                >
                int c=2,d=4;
                int a[]={c,d};
                >
                gave "Illegal Initialization" error when compiled with Turbo C
                But the error was supressed in gcc.
                >
                Please turn op the warnings.
                If the declarations are on file scope (i.e. not inside a function)
                gcc -ansi -pedantic -W -Wall -O
                gives a series of errors. Never trust the compiler suppressing errors.
                --
                Coos

                Comment

                • Steve Summit

                  #9
                  Re: Object Initialization

                  Richard Heathfield wrote:
                  >sarathy said:
                  > auto int b[]={a,a++};
                  > works fine.
                  >
                  Someone told me that, in basketball, you have to bounce the
                  ball off the floor; you can't hold the ball and run around the
                  court. But I tried it and it works just fine. Obviously he
                  does not understand basketball!
                  -- Attr. Steve Summit
                  The original quote by Roger Miller (then at Verifone, not sure
                  where he is now) was

                  Somebody once told me that in basketball you can't hold
                  the ball and run. I got a basketball and tried it and it
                  worked just fine. He obviously didn't understand basketball.

                  [The original message is missing from google's cache, but see
                  <CKs26M.GoH@crd nns.crd.ge.com> , or http://groups.google.com/
                  group/comp.lang.c/msg/9c6529ae1b85f00 8 .]

                  My own attempts at pithy quotes here always involve traffic
                  lights, e.g. "Dabbling in undefined behavior is a little like
                  running a red light at 3am: you probably won't get caught,
                  but it's still wrong."
                  --
                  Steve Summit
                  scs@eskimo.com

                  Comment

                  Working...