Array initialisation

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

    Array initialisation

    HI

    Would i be able to initialize an entire array by:-

    int a[10] = 0

    if not, how old be able to do it, would i have to use some form of loop
    to set each individual element?

    Dan

  • Artie Gold

    #2
    Re: Array initialisation

    John Dibling wrote:[color=blue]
    > On Mon, 04 Aug 2003 19:15:59 +0100, Danny <dan.schofield@ ntlworld.com>
    > wrote:
    >
    >[color=green]
    >>HI
    >>
    >>Would i be able to initialize an entire array by:-
    >>
    >>int a[10] = 0
    >>
    >>if not, how old be able to do it, would i have to use some form of loop
    >>to set each individual element?
    >>
    >>Dan[/color]
    >
    >
    > The code you have above will initialize the first element; all others
    > will be uninitialized (eg, garbage). You can do this:
    >
    > int a[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};[/color]

    int a[10] = {0};

    would be sufficient.
    [color=blue]
    >
    > or use a loop as you suggested.
    >[/color]

    HTH,
    --ag


    --
    Artie Gold -- Austin, Texas

    Comment

    • Rob Williscroft

      #3
      Re: Array initialisation

      Danny wrote in news:3F2EA2DF.9 020008@ntlworld .com:
      [color=blue]
      > HI
      >
      > Would i be able to initialize an entire array by:-
      >
      > int a[10] = 0[/color]

      int a[10] = {};
      [color=blue]
      >
      > if not, how old be able to do it, would i have to use some form of loop
      > to set each individual element?[/color]

      The above initializer causes all of a's elements to be default
      initialized, in the case of an int this means 0, so you get what
      you want.

      Note that:

      int b[3] = { 1 };
      is the same as
      int b[3] = { 1, 0, 0 };

      HTH

      Rob.
      --

      Comment

      • Andrey Tarasevich

        #4
        Re: Array initialisation

        Artie Gold wrote:[color=blue]
        > ...[color=green]
        >> The code you have above will initialize the first element; all others
        >> will be uninitialized (eg, garbage). You can do this:
        >>
        >> int a[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};[/color]
        >
        > int a[10] = {0};
        >
        > would be sufficient.
        > ...[/color]

        As Rob noted in his message, even

        int a[10] = {};

        would be sufficient.

        --
        Best regards,
        Andrey Tarasevich
        Brainbench C and C++ Programming MVP

        Comment

        • John Dibling

          #5
          Re: Array initialisation

          On Mon, 04 Aug 2003 13:27:41 -0700, Andrey Tarasevich
          <andreytarasevi ch@hotmail.com> wrote:
          [color=blue]
          >Artie Gold wrote:[color=green]
          >> ...[color=darkred]
          >>> The code you have above will initialize the first element; all others
          >>> will be uninitialized (eg, garbage). You can do this:
          >>>
          >>> int a[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};[/color]
          >>
          >> int a[10] = {0};
          >>
          >> would be sufficient.
          >> ...[/color]
          >
          >As Rob noted in his message, even
          >
          > int a[10] = {};
          >
          >would be sufficient.[/color]

          I don't think that's correct...

          8.5.1.7 :

          "If there are fewer initializers in the list than there are memvers in
          the aggregate, then each member not explicitly initialized shall be
          default initialized."

          Doesn't this mean anything not explicitly initialized would be
          uninitialized in this case?


          </dib>
          John Dibling
          Witty banter omitted for your protection

          Comment

          • Victor Bazarov

            #6
            Re: Array initialisation

            "John Dibling" <dib@substitute _my_full_last_n ame_here.com> wrote...[color=blue]
            > On Mon, 04 Aug 2003 13:27:41 -0700, Andrey Tarasevich
            > <andreytarasevi ch@hotmail.com> wrote:
            >[color=green]
            > >Artie Gold wrote:[color=darkred]
            > >> ...
            > >>> The code you have above will initialize the first element; all others
            > >>> will be uninitialized (eg, garbage). You can do this:
            > >>>
            > >>> int a[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            > >>
            > >> int a[10] = {0};
            > >>
            > >> would be sufficient.
            > >> ...[/color]
            > >
            > >As Rob noted in his message, even
            > >
            > > int a[10] = {};
            > >
            > >would be sufficient.[/color]
            >
            > I don't think that's correct...
            >
            > 8.5.1.7 :
            >
            > "If there are fewer initializers in the list than there are memvers in
            > the aggregate, then each member not explicitly initialized shall be
            > default initialized."
            >
            > Doesn't this mean anything not explicitly initialized would be
            > uninitialized in this case?[/color]

            Just scroll a few paragraphs back and read 8.5/5. For a POD to
            default-initialise means to zero-initialise. 'int' is a POD.

            Victor


            Comment

            • Andrey Tarasevich

              #7
              Re: Array initialisation

              John Dibling wrote:[color=blue][color=green]
              >> ...
              >>As Rob noted in his message, even
              >>
              >> int a[10] = {};
              >>
              >>would be sufficient.[/color]
              >
              > I don't think that's correct...
              >
              > 8.5.1.7 :
              >
              > "If there are fewer initializers in the list than there are memvers in
              > the aggregate, then each member not explicitly initialized shall be
              > default initialized."[/color]

              Actually, the above is exactly what makes the '{}' initializer
              sufficient in this case. I don't see what in this quote makes you think
              that the initialization is incorrect.
              [color=blue]
              > Doesn't this mean anything not explicitly initialized would be
              > uninitialized in this case?[/color]

              No, it means exactly what it says: everything not explicitly initialized
              will be _default_ _initialized_. Default initialization for 'int's means
              zero initialization. See a reference in Victor's message. Take a look at
              8.5.1/8 as well.

              --
              Best regards,
              Andrey Tarasevich
              Brainbench C and C++ Programming MVP

              Comment

              • Web Developer

                #8
                Re: Array initialisation

                > >> ...[color=blue][color=green][color=darkred]
                > >>As Rob noted in his message, even
                > >>
                > >> int a[10] = {};
                > >>
                > >>would be sufficient.[/color]
                > >
                > > I don't think that's correct...
                > >
                > > 8.5.1.7 :
                > >
                > > "If there are fewer initializers in the list than there are memvers in
                > > the aggregate, then each member not explicitly initialized shall be
                > > default initialized."[/color]
                >
                > Actually, the above is exactly what makes the '{}' initializer
                > sufficient in this case. I don't see what in this quote makes you think
                > that the initialization is incorrect.
                >[color=green]
                > > Doesn't this mean anything not explicitly initialized would be
                > > uninitialized in this case?[/color]
                >
                > No, it means exactly what it says: everything not explicitly initialized
                > will be _default_ _initialized_. Default initialization for 'int's means
                > zero initialization. See a reference in Victor's message. Take a look at
                > 8.5.1/8 as well.[/color]

                I noted your quote on a paragraph from some source:

                8.5.1.7 :

                "If there are fewer initializers in the list than there are memvers in
                the aggregate, then each member not explicitly initialized shall be
                default initialized."

                which I assume to be some online C++ reference. Where can I view this for
                myself?


                Regards
                WD


                Comment

                • Josephine Schafer

                  #9
                  Re: Array initialisation


                  "Web Developer" <nospam@hotmail .com> wrote in message
                  news:3f2f8452_1 @news.iprimus.c om.au...[color=blue][color=green][color=darkred]
                  > > >> ...
                  > > >>As Rob noted in his message, even
                  > > >>
                  > > >> int a[10] = {};
                  > > >>
                  > > >>would be sufficient.
                  > > >
                  > > > I don't think that's correct...
                  > > >
                  > > > 8.5.1.7 :
                  > > >
                  > > > "If there are fewer initializers in the list than there are memvers in
                  > > > the aggregate, then each member not explicitly initialized shall be
                  > > > default initialized."[/color]
                  > >
                  > > Actually, the above is exactly what makes the '{}' initializer
                  > > sufficient in this case. I don't see what in this quote makes you think
                  > > that the initialization is incorrect.
                  > >[color=darkred]
                  > > > Doesn't this mean anything not explicitly initialized would be
                  > > > uninitialized in this case?[/color]
                  > >
                  > > No, it means exactly what it says: everything not explicitly initialized
                  > > will be _default_ _initialized_. Default initialization for 'int's means
                  > > zero initialization. See a reference in Victor's message. Take a look at
                  > > 8.5.1/8 as well.[/color]
                  >
                  > I noted your quote on a paragraph from some source:
                  >
                  > 8.5.1.7 :
                  >
                  > "If there are fewer initializers in the list than there are memvers in
                  > the aggregate, then each member not explicitly initialized shall be
                  > default initialized."
                  >
                  > which I assume to be some online C++ reference. Where can I view this for
                  > myself?
                  >[/color]
                  The C++ standard.



                  Comment

                  • J. Campbell

                    #10
                    Re: Array initialisation

                    Rob Williscroft <rtw@freenet.RE MOVE.co.uk> wrote in message news:<Xns93CDC9 6C98109ukcoREMO VEfreenetrtw@19 5.129.110.130>. ..[color=blue]
                    > Danny wrote in news:3F2EA2DF.9 020008@ntlworld .com:
                    >[color=green]
                    > > HI
                    > >
                    > > Would i be able to initialize an entire array by:-
                    > >
                    > > int a[10] = 0[/color]
                    >
                    > int a[10] = {};
                    >[/color]

                    Thanks for the tip...I'd been using a bunch of loops to accomplish
                    same.

                    One question. If I have an array as part of a class, initializing to
                    zero using empty braces doesn't work. eg

                    class arrayholder{
                    int a[100] = {};
                    };

                    doesn't work. I have to use:

                    class arrayholder{
                    int a[100];
                    };

                    arrayholder::ar rayholder(){
                    for(int i = 0; i<100; i++)
                    a[i] = 0;
                    }

                    and initialize it in the constructor. However, I'd like to do
                    something like:
                    a[] = {};

                    in the constructor. any ideas?


                    Thanks

                    Comment

                    • John Dibling

                      #11
                      Re: Array initialisation

                      On Tue, 5 Aug 2003 19:48:17 +0930, "Web Developer"
                      <nospam@hotmail .com> wrote:
                      [color=blue]
                      >
                      > which I assume to be some online C++ reference. Where can I view this for
                      >myself?
                      >
                      >
                      >Regards
                      >WD
                      >[/color]

                      This is a reference to the ISO C++ standard document, which describes
                      the language in detail. AFAIK, there is no onlne location to view
                      this document; it is copyrighted material, and you must purchase a
                      copy. Here's one place where you can get one:



                      </dib>
                      John Dibling
                      Witty banter omitted for your protection

                      Comment

                      • Victor Bazarov

                        #12
                        Re: Array initialisation

                        "J. Campbell" <mango_maniac@y ahoo.com> wrote...[color=blue]
                        > Rob Williscroft <rtw@freenet.RE MOVE.co.uk> wrote in message[/color]
                        news:<Xns93CDC9 6C98109ukcoREMO VEfreenetrtw@19 5.129.110.130>. ..[color=blue][color=green]
                        > > Danny wrote in news:3F2EA2DF.9 020008@ntlworld .com:
                        > >[color=darkred]
                        > > > HI
                        > > >
                        > > > Would i be able to initialize an entire array by:-
                        > > >
                        > > > int a[10] = 0[/color]
                        > >
                        > > int a[10] = {};
                        > >[/color]
                        >
                        > Thanks for the tip...I'd been using a bunch of loops to accomplish
                        > same.
                        >
                        > One question. If I have an array as part of a class, initializing to
                        > zero using empty braces doesn't work. eg
                        >
                        > class arrayholder{
                        > int a[100] = {};
                        > };
                        >
                        > doesn't work.[/color]

                        Of course it doesn't. You're not in Java any more. The class
                        definition can contain only _declarations_ of data members, with
                        a simple exception: a static const of integral type is allowed
                        to be initialised there.
                        [color=blue]
                        > I have to use:
                        >
                        > class arrayholder{
                        > int a[100];
                        > };
                        >
                        > arrayholder::ar rayholder(){
                        > for(int i = 0; i<100; i++)
                        > a[i] = 0;
                        > }
                        >
                        > and initialize it in the constructor. However, I'd like to do
                        > something like:
                        > a[] = {};
                        >
                        > in the constructor. any ideas?[/color]

                        Nope. That's the limitation of the language.

                        Victor


                        Comment

                        • Adam Fineman

                          #13
                          Re: Array initialisation

                          J. Campbell wrote:
                          <snip>[color=blue]
                          > One question. If I have an array as part of a class, initializing to
                          > zero using empty braces doesn't work. eg
                          > <snip>[/color]
                          [color=blue]
                          > However, I'd like to do
                          > something like:
                          > a[] = {};
                          >
                          > in the constructor. any ideas?
                          >[/color]

                          Perhaps you should consider using a vector, for this and many other
                          reasons. (See the FAQ for comp.lang.c++ for more reasons.)

                          In your particular case, your code could look like this:

                          #include <vector>

                          class ArrayHolder
                          {
                          public:
                          ArrayHolder()
                          : d_v(10, 0)
                          { /* no code needed for vector initialization */ }
                          private:
                          std::vector<int > d_v;
                          };

                          The vector constructor I used above will initialize the vector with 10 '0's.

                          I understand that you need to know how arrays work in C++, as you need
                          to understand the basics. However, STL containers are usually a better
                          choice than arrays in C++.

                          - Adam

                          Comment

                          • Rob Williscroft

                            #14
                            Re: Array initialisation

                            J. Campbell wrote in
                            news:b97c86e1.0 308050646.1aab3 e5f@posting.goo gle.com:
                            [color=blue]
                            > Rob Williscroft <rtw@freenet.RE MOVE.co.uk> wrote in message
                            > news:<Xns93CDC9 6C98109ukcoREMO VEfreenetrtw@19 5.129.110.130>. ..[color=green]
                            >> Danny wrote in news:3F2EA2DF.9 020008@ntlworld .com:
                            >>[color=darkred]
                            >> > HI
                            >> >
                            >> > Would i be able to initialize an entire array by:-
                            >> >
                            >> > int a[10] = 0[/color]
                            >>
                            >> int a[10] = {};
                            >>[/color]
                            >
                            > Thanks for the tip...I'd been using a bunch of loops to accomplish
                            > same.
                            >
                            > One question. If I have an array as part of a class, initializing to
                            > zero using empty braces doesn't work. eg
                            >
                            > class arrayholder{
                            > int a[100] = {};
                            > };
                            >
                            > doesn't work. I have to use:
                            >
                            > class arrayholder{
                            > int a[100];
                            > };
                            >
                            > arrayholder::ar rayholder(){
                            > for(int i = 0; i<100; i++)
                            > a[i] = 0;
                            > }
                            >
                            > and initialize it in the constructor. However, I'd like to do
                            > something like:
                            > a[] = {};
                            >[/color]


                            struct arrayHolderBase
                            {
                            int a[100];
                            };

                            class arrayHolder: arrayHolderBase
                            {

                            public:

                            arrayHolder() : arrayHolderBase () {}
                            };


                            The important part of the above is: "... : arrayHolderBase () { ..."
                            bit. This explicit call of the POD's (arrayHolderBas e's) default
                            constructor causes the arrayHolderBase sub object of arrayHolder
                            to be defualt initialized (all fields/elements set to 0).

                            Just for fun I compiled the following:

                            #include <iostream>

                            struct arrayHolder
                            {
                            char a[100];
                            arrayHolder() : a() {}
                            };

                            void test()
                            {
                            arrayHolder a;

                            for (int i = 0; i < 100; ++i)
                            {
                            if (a.a[i])
                            {
                            std::cerr << "non 0 at " << i << "\n";
                            return;
                            }
                            }
                            }

                            void set()
                            {
                            arrayHolder a;

                            for (int i = 0; i < 100; ++i)
                            {
                            a.a[i] = i + 20;
                            }
                            }

                            int main()
                            {
                            set();
                            test();
                            std::cerr << "OK";
                            }

                            I fouund the results a bit suprising, No Errors/Warning's
                            (aka diagnostics) with any of the 3 compilers I tried.

                            g++ (gcc 3.2 MingW) : OK
                            msvc (7.1) : non 0 at 0 - OK
                            bcc32 (borland): non 0 at 0 - OK

                            I was really expecting (hopeing for) a diagnostic, BTW I used
                            -W -Wall -pedantic with g++.

                            I suspect all 3 compilers are conforming implementation' s (in this
                            regard) of our beloved C++ Standard.

                            Rob.
                            --

                            Comment

                            • Andrey Tarasevich

                              #15
                              Re: Array initialisation

                              Rob Williscroft wrote:[color=blue]
                              > ...
                              > struct arrayHolderBase
                              > {
                              > int a[100];
                              > };
                              >
                              > class arrayHolder: arrayHolderBase
                              > {
                              >
                              > public:
                              >
                              > arrayHolder() : arrayHolderBase () {}
                              > };
                              >
                              >
                              > The important part of the above is: "... : arrayHolderBase () { ..."
                              > bit. This explicit call of the POD's (arrayHolderBas e's) default
                              > constructor causes the arrayHolderBase sub object of arrayHolder
                              > to be defualt initialized (all fields/elements set to 0).[/color]

                              Strictly speaking, in standard terminology the '... : arrayHolderBase ()
                              { ...' bit is not and explicit call to 'arrayHolderBas e' constructor.
                              The '()' is just a form of member initializer which causes this member
                              to be default-initialized. No constructors are involved in this process.
                              [color=blue]
                              > Just for fun I compiled the following:
                              >
                              > #include <iostream>
                              >
                              > struct arrayHolder
                              > {
                              > char a[100];
                              > arrayHolder() : a() {}
                              > };
                              >
                              > void test()
                              > {
                              > arrayHolder a;
                              >
                              > for (int i = 0; i < 100; ++i)
                              > {
                              > if (a.a[i])
                              > {
                              > std::cerr << "non 0 at " << i << "\n";
                              > return;
                              > }
                              > }
                              > }
                              >
                              > void set()
                              > {
                              > arrayHolder a;
                              >
                              > for (int i = 0; i < 100; ++i)
                              > {
                              > a.a[i] = i + 20;
                              > }
                              > }
                              >
                              > int main()
                              > {
                              > set();
                              > test();
                              > std::cerr << "OK";
                              > }
                              >
                              > I fouund the results a bit suprising, No Errors/Warning's
                              > (aka diagnostics) with any of the 3 compilers I tried.[/color]

                              There shouldn't be any. The code is legal.
                              [color=blue]
                              > g++ (gcc 3.2 MingW) : OK
                              > msvc (7.1) : non 0 at 0 - OK
                              > bcc32 (borland): non 0 at 0 - OK
                              >
                              > I was really expecting (hopeing for) a diagnostic, BTW I used
                              > -W -Wall -pedantic with g++.
                              >
                              > I suspect all 3 compilers are conforming implementation' s (in this
                              > regard) of our beloved C++ Standard.[/color]

                              No. Looks like only 'g++' was able to produce the correct result. The
                              'msvc' and 'bcc32' are not conforming in this respect. As for 'msvc',
                              this problem existed in version 6 as well.

                              --
                              Best regards,
                              Andrey Tarasevich
                              Brainbench C and C++ Programming MVP

                              Comment

                              Working...