re-setting an array

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

    re-setting an array

    Is there a way to reset all elements of an array with a single instruction?
    I want to set all elements to zero. Currently looping to do so.

    thx,
    Scott Kelley


  • Patrik Stellmann

    #2
    Re: re-setting an array

    > Is there a way to reset all elements of an array with a single instruction?[color=blue]
    > I want to set all elements to zero. Currently looping to do so.[/color]
    setting to zero could be done with
    void *memset(void *dest, int c, size_t count);

    the code could look like:
    memset(intArray , 0, sizeof(int) * arraySize);

    Very fast but you have to take care of the correct byte-count to be set
    to zero yourself!

    Comment

    • Ioannis Vranos

      #3
      Re: re-setting an array

      Scott Kelley wrote:[color=blue]
      > Is there a way to reset all elements of an array with a single instruction?
      > I want to set all elements to zero. Currently looping to do so.[/color]



      memset() defined in <cstring>.

      Check this: http://www.cplusplus.com/ref/cstring/index.html






      Regards,

      Ioannis Vranos

      Comment

      • John Harrison

        #4
        Re: re-setting an array


        "Scott Kelley" <scottk@iccom.c om> wrote in message
        news:KqidnYJalq cXXn7d4p2dnA@ce nturytel.net...[color=blue]
        > Is there a way to reset all elements of an array with a single[/color]
        instruction?[color=blue]
        > I want to set all elements to zero. Currently looping to do so.
        >[/color]

        If you are talking about integers then memset.

        If you are talking about floating point then std::fill or std::fill_n. The
        advantage of std::fill and std::fill_n is that they work with many
        different types, values and data structures, but for integer arrays set to
        zero you aren't going to get any more efficient then memset.

        john


        Comment

        • Ioannis Vranos

          #5
          Re: re-setting an array

          John Harrison wrote:
          [color=blue]
          > If you are talking about integers then memset.
          >
          > If you are talking about floating point then std::fill or std::fill_n. The
          > advantage of std::fill and std::fill_n is that they work with many
          > different types, values and data structures, but for integer arrays set to
          > zero you aren't going to get any more efficient then memset.[/color]



          Actually, memset() is for values in the range of unsigned char (it fills
          bytes). I had forgotten about the fill() family. fill() seems more
          elegant, so if not needed i would suggest the OP to use the fill()
          family, and use memset() only when he has to.






          Regards,

          Ioannis Vranos

          Comment

          • Ioannis Vranos

            #6
            Re: re-setting an array

            Ioannis Vranos wrote:
            [color=blue]
            > Scott Kelley wrote:
            >[color=green]
            >> Is there a way to reset all elements of an array with a single
            >> instruction?
            >> I want to set all elements to zero. Currently looping to do so.[/color]
            >
            >
            >
            >
            > memset() defined in <cstring>.
            >
            > Check this: http://www.cplusplus.com/ref/cstring/index.html[/color]



            As i mentioned in another reply, better use the fill() family instead,
            unless you have to use memset().


            Check this: http://h30097.www3.hp.com/cplus/fill_n_3c__std.htm






            Regards,

            Ioannis Vranos

            Comment

            • John Harrison

              #7
              Re: re-setting an array


              "Ioannis Vranos" <ivr@guesswh.at .grad.com> wrote in message
              news:cc0ihl$2as q$1@ulysses.noc .ntua.gr...[color=blue]
              > John Harrison wrote:
              >[color=green]
              > > If you are talking about integers then memset.
              > >
              > > If you are talking about floating point then std::fill or std::fill_n.[/color][/color]
              The[color=blue][color=green]
              > > advantage of std::fill and std::fill_n is that they work with many
              > > different types, values and data structures, but for integer arrays set[/color][/color]
              to[color=blue][color=green]
              > > zero you aren't going to get any more efficient then memset.[/color]
              >
              >
              >
              > Actually, memset() is for values in the range of unsigned char (it fills
              > bytes).[/color]

              True but I have never worked on a platform where integer zero wasn't all
              bits zero, so I would happily use memset for integers.

              john


              Comment

              • Ioannis Vranos

                #8
                Re: re-setting an array

                John Harrison wrote:
                [color=blue][color=green]
                >>Actually, memset() is for values in the range of unsigned char (it fills
                >>bytes).[/color]
                >
                >
                > True but I have never worked on a platform where integer zero wasn't all
                > bits zero, so I would happily use memset for integers.[/color]



                Actually now that you mention it, memset() is only safe to be used in
                unsigned char sequences only. std::fill() family is the safe approach
                definitely.






                Regards,

                Ioannis Vranos

                Comment

                • Ioannis Vranos

                  #9
                  Re: re-setting an array

                  Ioannis Vranos wrote:
                  [color=blue]
                  > Actually now that you mention it, memset() is only safe to be used in
                  > unsigned char sequences[/color]

                  and to char and signed char sequences under some preconditions

                  [color=blue]
                  > only. std::fill() family is the safe approach
                  > definitely.[/color]






                  Regards,

                  Ioannis Vranos

                  Comment

                  • Jack Klein

                    #10
                    Re: re-setting an array

                    On Thu, 01 Jul 2004 11:38:25 +0300, Ioannis Vranos
                    <ivr@guesswh.at .grad.com> wrote in comp.lang.c++:
                    [color=blue]
                    > John Harrison wrote:
                    >[color=green]
                    > > If you are talking about integers then memset.
                    > >
                    > > If you are talking about floating point then std::fill or std::fill_n. The
                    > > advantage of std::fill and std::fill_n is that they work with many
                    > > different types, values and data structures, but for integer arrays set to
                    > > zero you aren't going to get any more efficient then memset.[/color]
                    >
                    >
                    >
                    > Actually, memset() is for values in the range of unsigned char (it fills
                    > bytes). I had forgotten about the fill() family. fill() seems more
                    > elegant, so if not needed i would suggest the OP to use the fill()
                    > family, and use memset() only when he has to.[/color]

                    No, this is quite silly, it's one of comp.lang.c's pedantic myths.

                    Actually it has to work for all the character types, because both C
                    and C++ guarantee that if a signed and unsigned variant of any integer
                    type contain a value that is within the range of both, the bitwise
                    representation must be exactly the same.

                    So all bits 0 must represent the value 0 in both a signed and unsigned
                    char, and therefore also in a plain char.

                    Even beyond that, the C standard committee already has accepted a DR
                    to require wording in the next revision of the C standard specifically
                    stating that all bits 0 is a valid representation of the value 0 for
                    _all_ integer types.

                    I doubt if the C++ standard, which does not allow padding bits in
                    signed char where the C standard does, will ever suffer this nonsense
                    about all bits 0 not being the value 0 in any integer type.

                    On the other hand, for pointers or floating point types, and of course
                    for non-POD types, using memset() is not a good idea at all.

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

                    Comment

                    • Jack Klein

                      #11
                      Re: re-setting an array

                      On Thu, 01 Jul 2004 12:24:57 +0300, Ioannis Vranos
                      <ivr@guesswh.at .grad.com> wrote in comp.lang.c++:
                      [color=blue]
                      > Ioannis Vranos wrote:
                      >[color=green]
                      > > Actually now that you mention it, memset() is only safe to be used in
                      > > unsigned char sequences[/color]
                      >
                      > and to char and signed char sequences under some preconditions[/color]

                      Under no preconditions at all.

                      From paragraph 3 of 3.9.1 Fundamental types:

                      "The range of nonnegative values of a signed integer type is a
                      subrange of the corresponding unsigned integer type, and the value
                      representation of each corresponding signed/unsigned type shall be the
                      same."

                      If there is an implementation where all bits 0 is not the value 0 in a
                      char or signed char type, then whatever that implementation is it is
                      not C or C++.
                      [color=blue][color=green]
                      > > only. std::fill() family is the safe approach
                      > > definitely.[/color]
                      >
                      > Regards,
                      >
                      > Ioannis Vranos[/color]

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

                      Comment

                      • Ioannis Vranos

                        #12
                        Re: re-setting an array

                        Jack Klein wrote:
                        [color=blue]
                        > Under no preconditions at all.
                        >
                        > From paragraph 3 of 3.9.1 Fundamental types:
                        >
                        > "The range of nonnegative values of a signed integer type is a
                        > subrange of the corresponding unsigned integer type, and the value
                        > representation of each corresponding signed/unsigned type shall be the
                        > same."
                        >
                        > If there is an implementation where all bits 0 is not the value 0 in a
                        > char or signed char type, then whatever that implementation is it is
                        > not C or C++.[/color]


                        I was talking about values larger than numeric_limits< signed
                        char>::max() but within the range of unsigned char.

                        0 is safe for char/signed char/unsigned char always.






                        Regards,

                        Ioannis Vranos

                        Comment

                        • Ioannis Vranos

                          #13
                          Re: re-setting an array

                          Jack Klein wrote:
                          [color=blue]
                          > On Thu, 01 Jul 2004 11:38:25 +0300, Ioannis Vranos
                          > <ivr@guesswh.at .grad.com> wrote in comp.lang.c++:
                          >
                          >[color=green]
                          >>John Harrison wrote:
                          >>
                          >>[color=darkred]
                          >>>If you are talking about integers then memset.
                          >>>
                          >>>If you are talking about floating point then std::fill or std::fill_n. The
                          >>>advantage of std::fill and std::fill_n is that they work with many
                          >>>different types, values and data structures, but for integer arrays set to
                          >>>zero you aren't going to get any more efficient then memset.[/color]
                          >>
                          >>
                          >>
                          >>Actually, memset() is for values in the range of unsigned char (it fills
                          >>bytes). I had forgotten about the fill() family. fill() seems more
                          >>elegant, so if not needed i would suggest the OP to use the fill()
                          >>family, and use memset() only when he has to.[/color]
                          >
                          >
                          > No, this is quite silly, it's one of comp.lang.c's pedantic myths.
                          >
                          > Actually it has to work for all the character types, because both C
                          > and C++ guarantee that if a signed and unsigned variant of any integer
                          > type contain a value that is within the range of both, the bitwise
                          > representation must be exactly the same.[/color]


                          Yes I agree that for 0 it works for all character types, *regardless* of
                          the representation (it doesn't matter if 0 is all bit zeros or otherwise).

                          [color=blue]
                          > So all bits 0 must represent the value 0 in both a signed and unsigned
                          > char, and therefore also in a plain char.[/color]


                          Bits don't matter here.


                          [color=blue]
                          > Even beyond that, the C standard committee already has accepted a DR
                          > to require wording in the next revision of the C standard specifically
                          > stating that all bits 0 is a valid representation of the value 0 for
                          > _all_ integer types.[/color]



                          Which is entirely off topic in here (otherwise expressed as who cares?). :-)



                          [color=blue]
                          >
                          > I doubt if the C++ standard, which does not allow padding bits in
                          > signed char where the C standard does, will ever suffer this nonsense
                          > about all bits 0 not being the value 0 in any integer type.[/color]



                          But this doesn't affect memset() in any way.



                          [color=blue]
                          >
                          > On the other hand, for pointers or floating point types, and of course
                          > for non-POD types, using memset() is not a good idea at all[/color]


                          Of course. And for integrals other than char/signed char/unsigned char.
                          And of course for values larger than numeric_limits< signed char>::max()
                          it shouldn't be used on signed chars, and for values larger than
                          numeric_limits< char>::max() it shouldn't be used in chars, and for
                          values larger than numeric_limits< unsigned char>::max() it shouldn't be
                          used on unsigned chars.


                          fill() family is better suitable for all purposes, unless we can't do
                          otherwise.






                          Regards,

                          Ioannis Vranos

                          Comment

                          • Alf P. Steinbach

                            #14
                            Re: re-setting an array

                            * Jack Klein:[color=blue]
                            > On Thu, 01 Jul 2004 12:24:57 +0300, Ioannis Vranos
                            > <ivr@guesswh.at .grad.com> wrote in comp.lang.c++:
                            >[color=green]
                            > > Ioannis Vranos wrote:
                            > >[color=darkred]
                            > > > Actually now that you mention it, memset() is only safe to be used in
                            > > > unsigned char sequences[/color]
                            > >
                            > > and to char and signed char sequences under some preconditions[/color]
                            >
                            > Under no preconditions at all.
                            >
                            > From paragraph 3 of 3.9.1 Fundamental types:
                            >
                            > "The range of nonnegative values of a signed integer type is a
                            > subrange of the corresponding unsigned integer type, and the value
                            > representation of each corresponding signed/unsigned type shall be the
                            > same."[/color]

                            It is worth noting that the standard defines the term "value
                            representation" in a para preceding this one.

                            It doesn't mean the representation of a value in terms of 0's and 1's...

                            This is a case where possibly the intent of the standard is different
                            from the actual wording, but anyway, it's not a good idea to use memset
                            for anything if it can be avoided, since it's dangerous in many ways:
                            the possibility of 0 not being represented by all bits 0 for some types,
                            the ease with which incorrect limits can be specified, the possibility
                            of being applied to what is actually non-contigous storage.

                            I'd advice the OP to use std::vector instead of raw arrays.

                            Safely & efficiently clearing a std::vector v is very very easy:

                            zeroAllElements In( v );

                            where zeroAllElements In can be defined as

                            template< typename T >
                            void zeroAllElements In( std::vector<T>& v )
                            {
                            std::size_t const size = v.size();
                            v.clear();
                            v.resize( size );
                            }

                            or

                            template< typename T >
                            void zeroAllElements In( std::vector<T>& v )
                            {
                            std::fill( v.begin(), v.end(), T() );
                            }

                            or whatever, just not memset.

                            --
                            A: Because it messes up the order in which people normally read text.
                            Q: Why is it such a bad thing?
                            A: Top-posting.
                            Q: What is the most annoying thing on usenet and in e-mail?

                            Comment

                            • Ioannis Vranos

                              #15
                              Re: re-setting an array

                              Alf P. Steinbach wrote:
                              [color=blue]
                              > This is a case where possibly the intent of the standard is different
                              > from the actual wording, but anyway, it's not a good idea to use memset
                              > for anything if it can be avoided, since it's dangerous in many ways:
                              > the possibility of 0 not being represented by all bits 0 for some types,[/color]



                              memset() has nothing to do with bits but with bytes. It sets them to a
                              value considering them as unsigned chars. For the value 0 this is safe
                              for all character types, but not for anything else.






                              Regards,

                              Ioannis Vranos

                              Comment

                              Working...