Clearing an array

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

    Clearing an array

    I have an array that I initialize to zero, like:

    Buffer[300] = {0x00};

    How do I in my code reset this array to all zeros ones more? Without running
    a whole for loop?

    Best Regards
    Terry


  • Robert Stankowic

    #2
    Re: Clearing an array


    "Terry Andersen" <terr@sea.com > schrieb im Newsbeitrag
    news:bk4eck$4dn $1@news.net.uni-c.dk...[color=blue]
    > I have an array that I initialize to zero, like:
    >
    > Buffer[300] = {0x00};
    >
    > How do I in my code reset this array to all zeros ones more? Without[/color]
    running[color=blue]
    > a whole for loop?
    >[/color]

    memset(Buffer, 0x00, sizeof Buffer);

    Robert


    Comment

    • pete

      #3
      Re: Clearing an array

      Terry Andersen wrote:[color=blue]
      >
      > I have an array that I initialize to zero, like:
      >
      > Buffer[300] = {0x00};
      >
      > How do I in my code reset this array to all zeros ones more?
      > Without running a whole for loop?[/color]

      A loop might be best.
      There's no single operation that will do it.

      Comment

      • pete

        #4
        Re: Clearing an array

        Robert Stankowic wrote:[color=blue]
        >
        > "Terry Andersen" <terr@sea.com > schrieb im Newsbeitrag
        > news:bk4eck$4dn $1@news.net.uni-c.dk...[color=green]
        > > I have an array that I initialize to zero, like:
        > >
        > > Buffer[300] = {0x00};
        > >
        > > How do I in my code reset this array to all zeros ones more?[/color]
        > Without running a whole for loop?[color=green]
        > >[/color]
        >
        > memset(Buffer, 0x00, sizeof Buffer);[/color]

        Not knowing the representation of ((*Buffer)0),
        memset() might or might not work right.

        Comment

        • Lew Pitcher

          #5
          Re: Clearing an array

          On Mon, 15 Sep 2003 15:26:35 +0200, "Terry Andersen" <terr@sea.com > wrote:
          [color=blue]
          >I have an array that I initialize to zero, like:
          >
          >Buffer[300] = {0x00};
          >
          >How do I in my code reset this array to all zeros ones more? Without running
          >a whole for loop?[/color]

          #include <string.h>

          memset(Buffer, 0, sizeof Buffer);
          [color=blue]
          >Best Regards
          >Terry
          >
          >[/color]

          --
          Lew Pitcher
          IT Consultant, Enterprise Technology Solutions
          Toronto Dominion Bank Financial Group

          (Opinions expressed are my own, not my employers')

          Comment

          • Ed Morton

            #6
            Re: Clearing an array



            Terry Andersen wrote:
            [color=blue]
            > I have an array that I initialize to zero, like:
            >
            > Buffer[300] = {0x00};
            >
            > How do I in my code reset this array to all zeros ones more? Without running
            > a whole for loop?
            >
            > Best Regards
            > Terry[/color]

            If you can put it inside a structure, you could create a zero-filled
            instance and assign your buffer to that, e.g. this:

            #include<stdio. h>
            #include<string .h>

            #define BUFFSIZE 9

            typedef struct {
            int Buffer[BUFFSIZE];
            } BUFFSTRUCT;

            static const BUFFSTRUCT ZeroBuffStruct;

            void prtBuf(int buf[])
            {
            int i;
            for (i=0; i<BUFFSIZE; i++)
            printf("%d",buf[i]);
            printf("\n");
            }

            int main()
            {
            BUFFSTRUCT BufStruct;
            int *Buffer = BufStruct.Buffe r;

            BufStruct = ZeroBuffStruct;
            prtBuf(Buffer);

            Buffer[3] = 4;
            prtBuf(Buffer);

            BufStruct = ZeroBuffStruct;
            prtBuf(Buffer);

            return 1;
            }

            produces the output:

            Buffer="0000000 0"
            Buffer="0004000 0"
            Buffer="0000000 0"

            If that's non-portable or has other problems, I'm sure someone will let
            us know....

            Regards,

            Ed.

            Comment

            • Thomas Pfaff

              #7
              Re: Clearing an array

              Lew.Pitcher@td. com (Lew Pitcher) writes:
              [color=blue]
              > On Mon, 15 Sep 2003 15:26:35 +0200, "Terry Andersen" <terr@sea.com > wrote:
              >[color=green]
              > >I have an array that I initialize to zero, like:
              > >
              > >Buffer[300] = {0x00};
              > >
              > >How do I in my code reset this array to all zeros ones more? Without running
              > >a whole for loop?[/color]
              >
              > #include <string.h>
              >
              > memset(Buffer, 0, sizeof Buffer);
              >[/color]

              That's not guaranteed to work unless `Buffer' is of type unsigned char, is it?

              Comment

              • Robert Stankowic

                #8
                Re: Clearing an array


                "pete" <pfiland@mindsp ring.com> schrieb im Newsbeitrag
                news:3F65C3AD.6 A6@mindspring.c om...[color=blue]
                > Robert Stankowic wrote:[color=green]
                > >
                > > "Terry Andersen" <terr@sea.com > schrieb im Newsbeitrag
                > > news:bk4eck$4dn $1@news.net.uni-c.dk...[color=darkred]
                > > > I have an array that I initialize to zero, like:
                > > >
                > > > Buffer[300] = {0x00};
                > > >
                > > > How do I in my code reset this array to all zeros ones more?[/color]
                > > Without running a whole for loop?[color=darkred]
                > > >[/color]
                > >
                > > memset(Buffer, 0x00, sizeof Buffer);[/color]
                >
                > Not knowing the representation of ((*Buffer)0),
                > memset() might or might not work right.[/color]

                You are ritght, of course. Somehow the 0x00 made me think of unsigned char.
                Shame on me.
                Robert


                Comment

                • Thomas Matthews

                  #9
                  Re: Clearing an array

                  Thomas Pfaff wrote:
                  [color=blue]
                  > Lew.Pitcher@td. com (Lew Pitcher) writes:
                  >
                  >[color=green]
                  >>On Mon, 15 Sep 2003 15:26:35 +0200, "Terry Andersen" <terr@sea.com > wrote:
                  >>
                  >>[color=darkred]
                  >>>I have an array that I initialize to zero, like:
                  >>>
                  >>>Buffer[300] = {0x00};
                  >>>
                  >>>How do I in my code reset this array to all zeros ones more? Without running
                  >>>a whole for loop?[/color]
                  >>
                  >> #include <string.h>
                  >>
                  >> memset(Buffer, 0, sizeof Buffer);
                  >>[/color]
                  >
                  >
                  > That's not guaranteed to work unless `Buffer' is of type unsigned char, is it?[/color]

                  It will work on any platform where the data type of buffer can be set
                  to zero in all bytes. If the platform has a data type where the value
                  of zero is not zeros in all bytes, then it won't work.

                  --
                  Thomas Matthews

                  C++ newsgroup welcome message:

                  C++ Faq: http://www.parashift.com/c++-faq-lite
                  C Faq: http://www.eskimo.com/~scs/c-faq/top.html
                  alt.comp.lang.l earn.c-c++ faq:

                  Other sites:
                  http://www.josuttis.com -- C++ STL Library book

                  Comment

                  • Christopher Benson-Manica

                    #10
                    Re: Clearing an array

                    pete <pfiland@mindsp ring.com> spoke thus:
                    [color=blue]
                    > Not knowing the representation of ((*Buffer)0),
                    > memset() might or might not work right.[/color]

                    Why is this so? Are you saying that if Buffer were

                    long Buffer[300];

                    that memset wouldn't work, even if you did

                    memset( Buffer, 0, 300 * sizeof(long) );

                    ?

                    --
                    Christopher Benson-Manica | Jumonji giri, for honour.
                    ataru(at)cybers pace.org |

                    Comment

                    • Thomas Pfaff

                      #11
                      Re: Clearing an array

                      Christopher Benson-Manica <ataru@nospam.c yberspace.org> writes:
                      [color=blue]
                      > pete <pfiland@mindsp ring.com> spoke thus:
                      >[color=green]
                      > > Not knowing the representation of ((*Buffer)0),
                      > > memset() might or might not work right.[/color]
                      >
                      > Why is this so? Are you saying that if Buffer were
                      >
                      > long Buffer[300];
                      >
                      > that memset wouldn't work, even if you did
                      >
                      > memset( Buffer, 0, 300 * sizeof(long) );[/color]

                      See `Defect Report #263':

                      Comment

                      • Lew Pitcher

                        #12
                        Re: Clearing an array

                        On 15 Sep 2003 17:13:24 +0200, Thomas Pfaff <thomas.pfaff@t iscali.no> wrote:
                        [color=blue]
                        >Lew.Pitcher@td .com (Lew Pitcher) writes:
                        >[color=green]
                        >> On Mon, 15 Sep 2003 15:26:35 +0200, "Terry Andersen" <terr@sea.com > wrote:
                        >>[color=darkred]
                        >> >I have an array that I initialize to zero, like:
                        >> >
                        >> >Buffer[300] = {0x00};
                        >> >
                        >> >How do I in my code reset this array to all zeros ones more? Without running
                        >> >a whole for loop?[/color]
                        >>
                        >> #include <string.h>
                        >>
                        >> memset(Buffer, 0, sizeof Buffer);
                        >>[/color]
                        >
                        >That's not guaranteed to work unless `Buffer' is of type unsigned char, is it?[/color]

                        You are correct, of course.

                        --
                        Lew Pitcher
                        IT Consultant, Enterprise Technology Solutions
                        Toronto Dominion Bank Financial Group

                        (Opinions expressed are my own, not my employers')

                        Comment

                        • Joe Wright

                          #13
                          Re: Clearing an array

                          Ed Morton wrote:[color=blue]
                          >
                          > Terry Andersen wrote:
                          >[color=green]
                          > > I have an array that I initialize to zero, like:
                          > >
                          > > Buffer[300] = {0x00};
                          > >
                          > > How do I in my code reset this array to all zeros ones more? Without running
                          > > a whole for loop?
                          > >
                          > > Best Regards
                          > > Terry[/color]
                          >
                          > If you can put it inside a structure, you could create a zero-filled
                          > instance and assign your buffer to that, e.g. this:
                          >
                          > #include<stdio. h>
                          > #include<string .h>
                          >
                          > #define BUFFSIZE 9
                          >
                          > typedef struct {
                          > int Buffer[BUFFSIZE];
                          > } BUFFSTRUCT;
                          >
                          > static const BUFFSTRUCT ZeroBuffStruct;
                          >
                          > void prtBuf(int buf[])
                          > {
                          > int i;
                          > for (i=0; i<BUFFSIZE; i++)
                          > printf("%d",buf[i]);
                          > printf("\n");
                          > }
                          >
                          > int main()
                          > {
                          > BUFFSTRUCT BufStruct;
                          > int *Buffer = BufStruct.Buffe r;
                          >
                          > BufStruct = ZeroBuffStruct;
                          > prtBuf(Buffer);
                          >
                          > Buffer[3] = 4;
                          > prtBuf(Buffer);
                          >
                          > BufStruct = ZeroBuffStruct;
                          > prtBuf(Buffer);
                          >
                          > return 1;
                          > }
                          >
                          > produces the output:
                          >
                          > Buffer="0000000 0"
                          > Buffer="0004000 0"
                          > Buffer="0000000 0"
                          >
                          > If that's non-portable or has other problems, I'm sure someone will let
                          > us know....
                          >
                          > Regards,
                          >
                          > Ed.[/color]

                          You're crazy! I love the way your mind works. Really! How about this..

                          #include <stdio.h>

                          #define SA 10

                          typedef struct arr {
                          int arr[SA];
                          } arr;

                          arr st_arr;


                          int main(void) {
                          int array[SA] = { 0 }; /* initialize array */
                          printf("%5d\n", array[0]); /* show that array[0] == 0 */
                          array[0] = SA; /* change it to something else */
                          printf("%5d\n", array[0]); /* show the change */
                          printf("%5d\n", st_arr.arr[0]); /* show that the struct was 0 */
                          printf("%5lu%5l u\n", sizeof array, sizeof st_arr);
                          *((arr *)array) = st_arr; /* assign the struct to the local array */
                          printf("%5d\n", array[0]); /* show that it worked */
                          return 0;
                          }
                          C makes arrays difficult. Their expression results in a pointer to its
                          first element, they cannot be passed by value. Shucks. But what of a
                          struct consisting of an array? You can do anything with the struct that
                          you can with the array. What I have not realized until today is, you can
                          assign the 'value' of a struct (the whole thing) to a local array of the
                          same size (carefully), as above.

                          *((arr *)array) = st_arr; /* assign the struct to the local array */

                          This works (I think) because array in this context is pointer. Casting
                          it to a struct pointer and dereferencing it creates perfect lvalue
                          target for the assignment of a struct.

                          Have I been asleep all these years? Have I made a new discovery? Thanks
                          Ed.
                          --
                          Joe Wright mailto:joewwrig ht@earthlink.ne t
                          "Everything should be made as simple as possible, but not simpler."
                          --- Albert Einstein ---

                          Comment

                          • Thomas Pfaff

                            #14
                            Re: Clearing an array

                            Joe Wright <joewwright@ear thlink.net> writes:[color=blue]
                            > C makes arrays difficult. Their expression results in a pointer to its
                            > first element, they cannot be passed by value.[/color]

                            Well since the value of an array is a pointer to its initial element,
                            I'd say arrays are indeed being passed by value ;-)

                            Comment

                            • Martin Dickopp

                              #15
                              Re: Clearing an array

                              Thomas Pfaff <thomas.pfaff@t iscali.no> writes:
                              [color=blue]
                              > Lew.Pitcher@td. com (Lew Pitcher) writes:
                              >[color=green]
                              > > #include <string.h>
                              > >
                              > > memset(Buffer, 0, sizeof Buffer);[/color]
                              >
                              > That's not guaranteed to work unless `Buffer' is of type unsigned char, is it?[/color]

                              As you correctly point out elsewhere in this thread, it is guaranteed to
                              work for all integer types according to Defect Report #263. :)

                              Martin

                              Comment

                              Working...