array alignment

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

    #1

    array alignment

    If I have

    sometype ar[A][B];

    then does the following command clear the whole array to zero ?

    memset( ar, 0, A*B*sizeof(some type) );

    Thanks,

    Vu





  • Dario (drinking coffee in the office…)

    #2
    Re: array alignment

    Vu Pham wrote:
    [color=blue]
    > If I have
    >
    > sometype ar[A][B];
    >
    > then does the following command clear the whole array to zero ?
    >
    > memset( ar, 0, A*B*sizeof(some type) );[/color]

    memset(ar, 0, sizeof ar);

    Comment

    • Vu Pham

      #3
      Re: array alignment


      "Dario (drinking coï¬?ee in the oï¬fceâ?¦)" <dario@despamme d.com> wrote in
      message news:ca7pjg$3vd $1@carabinieri. cs.interbusines s.it...[color=blue]
      > Vu Pham wrote:
      >[color=green]
      > > If I have
      > >
      > > sometype ar[A][B];
      > >
      > > then does the following command clear the whole array to zero ?
      > >
      > > memset( ar, 0, A*B*sizeof(some type) );[/color]
      >
      > memset(ar, 0, sizeof ar);[/color]

      Thanks, but what I asked is if "memset( ar, 0, A*B*sizeof(some type) );"
      works. I just want to understand how array aligned in different platforms.

      The reason is sometimes I cast my array to , say , char * and access it
      from there. Besides the problem of little/big endian, I would like to know
      if I will meet any other problem.

      Vu



      Comment

      • Barry Schwarz

        #4
        Re: array alignment

        On Wed, 9 Jun 2004 11:38:15 -0500, "Vu Pham" <vu@sivell.co m> wrote:
        [color=blue]
        >If I have
        >
        >sometype ar[A][B];
        >
        >then does the following command clear the whole array to zero ?
        >
        >memset( ar, 0, A*B*sizeof(some type) );
        >[/color]
        It will set every byte of the array to all bits 0. Whether or not all
        bits 0 is an acceptable value for an object of type sometype is
        implementation dependent. Even if it is an acceptable value, whether
        that value is the same as zero may also be implementation dependent
        (or even without meaning if, for example, sometype is a struct).


        <<Remove the del for email>>

        Comment

        • kal

          #5
          Re: array alignment

          "Vu Pham" <vu@sivell.co m> wrote in message news:<2iovh7Fpn unpU1@uni-berlin.de>...[color=blue]
          > "Dario (drinking coï¬?ee in the oï¬fceâ?¦)" <dario@despamme d.com> wrote in
          > message news:ca7pjg$3vd $1@carabinieri. cs.interbusines s.it...[color=green]
          > > Vu Pham wrote:
          > >[color=darkred]
          > > > If I have
          > > >
          > > > sometype ar[A][B];
          > > >
          > > > then does the following command clear the whole array to zero ?
          > > >
          > > > memset( ar, 0, A*B*sizeof(some type) );[/color]
          > >
          > > memset(ar, 0, sizeof ar);[/color]
          >
          > Thanks, but what I asked is if "memset( ar, 0, A*B*sizeof(some type) );"
          > works. I just want to understand how array aligned in different platforms.[/color]

          I have heard of the "alignment" considerations only with regard
          to structures, and not arrays.

          However, it seems that structures may have padding _at the end_
          to facilitate allocations of arrays of such structures. This
          implies that arrays have no "holes" between its elements.

          Please see: http://www.eskimo.com/~scs/C-faq/q2.13.html

          But your question is valid in that does the standard REQUIRE
          array elements to be sequentially allocated in memory? In
          other words, can the whole array be treated as an array of
          a different type whose size in bytes is the same?

          I would guess "yes" but I really don't know the answer to that.
          Perhaps someone with a good knowledge of the standard will post.

          Thanks for the question!

          Comment

          • Ralmin

            #6
            Re: array alignment

            "Vu Pham" <vu@sivell.co m> wrote:[color=blue]
            > If I have
            >
            > sometype ar[A][B];
            >
            > then does the following command clear the whole array to zero ?
            >
            > memset( ar, 0, A*B*sizeof(some type) );[/color]

            It sets every every bit of every byte in the array to zero. While that
            should be fine when sometype is any integer type, it is not necessarily
            correct for a floating-point or pointer type. The all-bits-zero may not be a
            valid representation of the value 0.0 or a null pointer.

            By the way,
            A * B * sizeof (sometype)
            A * B * sizeof **ar
            A * sizeof *ar
            sizeof ar
            are all equivalent.

            I believe this will correctly zero out every element of ar, no matter what
            type sometype is:

            size_t i, j;
            sometype temp = {0};
            for(i = 0; i < sizeof ar / sizeof *ar; i++)
            for(j = 0; j < sizeof *ar / sizeof **ar; j++)
            memcpy(&ar[i][j], &temp, sizeof **ar);

            --
            Simon.


            Comment

            • Vu Pham

              #7
              Re: array alignment

              Thanks everyboy.

              "Vu Pham" <vu@sivell.co m> wrote in message
              news:2iosidFpj0 qrU1@uni-berlin.de...[color=blue]
              > If I have
              >
              > sometype ar[A][B];
              >
              > then does the following command clear the whole array to zero ?
              >
              > memset( ar, 0, A*B*sizeof(some type) );
              >
              > Thanks,
              >
              > Vu
              >
              >
              >
              >
              >[/color]


              Comment

              • Peter Ammon

                #8
                Re: array alignment

                Vu Pham wrote:
                [color=blue]
                > If I have
                >
                > sometype ar[A][B];
                >
                > then does the following command clear the whole array to zero ?
                >
                > memset( ar, 0, A*B*sizeof(some type) );
                >
                > Thanks,
                >
                > Vu[/color]

                If you're not aware, you can write this:

                sometype ar[A][B] = {{0}};

                which will initialize the elements to zero, NULL, or whatever the analog
                for sometype is.

                If you were aware, carry on :)

                -Peter

                Comment

                • Dave Thompson

                  #9
                  Re: array alignment

                  ??
                  On Thu, 10 Jun 2004 10:03:33 GMT, "Ralmin" <news@ralminNOS PAM.cc>
                  wrote:
                  [color=blue]
                  > "Vu Pham" <vu@sivell.co m> wrote:[color=green]
                  > > If I have
                  > >
                  > > sometype ar[A][B];
                  > >
                  > > then does the following command clear the whole array to zero ?
                  > >
                  > > memset( ar, 0, A*B*sizeof(some type) );[/color]
                  >
                  > It sets every every bit of every byte in the array to zero. While that
                  > should be fine when sometype is any integer type, it is not necessarily[/color]

                  To be picky, it's actually guaranteed in C99 only for unsigned char
                  (pure binary, no sign, no padding) and in C89 only for arguably all
                  char flavors, but DR 263 has been accepted to guarantee it for all
                  integer types, so in practice yes it should be safe.
                  [color=blue]
                  > correct for a floating-point or pointer type. The all-bits-zero may not be a
                  > valid representation of the value 0.0 or a null pointer.
                  >
                  > By the way,
                  > A * B * sizeof (sometype)
                  > A * B * sizeof **ar
                  > A * sizeof *ar
                  > sizeof ar
                  > are all equivalent.
                  >[/color]
                  Subtle point: the last two are; the first two are equivalent to each
                  other, but possibly not to the last two if A*B has a (common) type
                  narrower than (size_t and) its value i.e. overflows.
                  [color=blue]
                  > I believe this will correctly zero out every element of ar, no matter what
                  > type sometype is:
                  >
                  > size_t i, j;
                  > sometype temp = {0};[/color]

                  Note: which is OK for a scalar or aggregate (or union) sometype,
                  whereas = 0 is (currently, pace Dan Pop) only good for a scalar.
                  [color=blue]
                  > for(i = 0; i < sizeof ar / sizeof *ar; i++)
                  > for(j = 0; j < sizeof *ar / sizeof **ar; j++)
                  > memcpy(&ar[i][j], &temp, sizeof **ar);[/color]

                  Or just ar[i][j] = temp.

                  Or several variants; sometimes I prefer to do a whole row at a time.

                  - David.Thompson1 at worldnet.att.ne t

                  Comment

                  Working...