How to traverse the items in an array ?

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

    How to traverse the items in an array ?

    I need a function that iterates over an array, like this one:

    int iterate_array (void* mem, size_t size, callback func)
    {
    char* p = (char*)mem;
    while(size-- 0) {
    func(p);
    p += size;
    }
    }

    but i'm not sure how to get the value of 'size'. First of course was
    the idea

    int a[10];
    iterate_array(a , sizeof(int), intfunc)

    but what if the data is 6 byte? Then its up to the compiler (or
    pragmas) it might be aligned to 8. How can i find this alignment. Will
    a (sizeof(type[2])/2) expression always return the correct size
    including alignment so that the generic iterator works ?

  • Richard Heathfield

    #2
    Re: How to traverse the items in an array ?

    llothar said:
    I need a function that iterates over an array, like this one:
    >
    int iterate_array (void* mem, size_t size, callback func)
    {
    char* p = (char*)mem;
    The cast is not required.
    while(size-- 0) {
    No, that won't do at all. It does not express your intent. See below:
    func(p);
    p += size;
    }
    }
    >
    but i'm not sure how to get the value of 'size'. First of course was
    the idea
    >
    int a[10];
    iterate_array(a , sizeof(int), intfunc)
    Why not sizeof a[0] ?
    but what if the data is 6 byte?
    So what?
    Then its up to the compiler (or pragmas) it might be aligned to 8.
    No, because array elements are contiguous. Here, I express your intent
    more clearly and correctly:

    typedef void (*callback)(voi d *);

    int iterate_array(v oid *arr, size_t nmemb, size_t size, callback func)
    {
    unsigned char *p = arr;
    while(nmemb--)
    {
    func(p);
    p += size;
    }
    return 0; /* presumably you had a reason for making this function
    return int, so fix this up when you're ready. */
    }

    Usage:

    int a[10] = {0};

    iterate_array(a , sizeof a / sizeof a[0], sizeof a[0], intfunc);

    Take a close look at qsort and bsearch. You may find it educational.

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

    email: rjh at the above domain, - www.

    Comment

    • llothar

      #3
      Re: How to traverse the items in an array ?

      No, that won't do at all. It does not express your intent. See below:

      Sorry this was a stupid error, i had no code snippet so i typed it
      here without thinking to much.
      (I'm not such a bad programmer).
      >
      Then its up to the compiler (or pragmas) it might be aligned to 8.
      >
      No, because array elements are contiguous. Here, I express your intent
      more clearly and correctly:
      Really? Okay this would be an easy answer to the question.
      And is this always true even in the different c compiler
      implementations , regardless of any used pragma settings?

      Comment

      • Richard Heathfield

        #4
        Re: How to traverse the items in an array ?

        llothar said:

        <snip>
        And is [the contiguity of array members] always true even in the
        different c compiler implementations , regardless of any used
        pragma settings?
        It is true for a conforming C implementation. It's entirely possible to
        stop an implementation from being conforming by using pragmas. For
        example, an implementation might supply support for a pragma whose
        meaning is "treat all + as - and all - as +". There's no accounting for
        pragmas.

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

        email: rjh at the above domain, - www.

        Comment

        • Richard Tobin

          #5
          Re: How to traverse the items in an array ?

          In article <1177336116.308 093.235980@l77g 2000hsb.googleg roups.com>,
          llothar <llothar@web.de wrote:
          >No, because array elements are contiguous.
          >Really? Okay this would be an easy answer to the question.
          >And is this always true even in the different c compiler
          >implementation s, regardless of any used pragma settings?
          It's true for all conforming compilers, but if you run them in a
          non-conforming mode anything might be true. And pragmas can have any
          effect.

          I have used a computer that used 5-byte floats, but there wouldn't be
          much point in that if they had to be padded to 8 bytes in arrays. And
          even if for some reason it was desirable, it could be done by having
          8-byte floats and only using 5 of the bytes in arithmetic operations.

          -- Richard
          --
          "Considerat ion shall be given to the need for as many as 32 characters
          in some alphabets" - X3.4, 1963.

          Comment

          • llothar

            #6
            Re: How to traverse the items in an array ?

            Really? Okay this would be an easy answer to the question.
            And is this always true even in the different c compiler
            implementations , regardless of any used pragma settings?
            Sorry to much bad home made alcohol yesterday.

            I meant the difference between a size of a single item and an array
            item. But with "sizeof a[0] " it should be clear to get this size (in
            the caller not in the callee. )

            I would guess there is an pragma, attribute that allows packing of the
            items inside the array or not.

            But i see that this is a case i can't catch anyway without having
            access to the original array declaration.

            Comment

            • etherzhhb@gmail.com

              #7
              Re: How to traverse the items in an array ?

              On Apr 23, 9:14 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
              iterate_array(a , sizeof a / sizeof a[0], sizeof a[0], intfunc);
              >
              what about when a is a pointer:
              int b[10] = {0};
              int *a = b;

              "sizeof a" could get nothing but the size of that pointer.


              Comment

              • Richard Heathfield

                #8
                Re: How to traverse the items in an array ?

                etherzhhb@gmail .com said:
                On Apr 23, 9:14 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                >
                >iterate_array( a, sizeof a / sizeof a[0], sizeof a[0], intfunc);
                >>
                what about when a is a pointer:
                What about when a is an Edsel?

                Check the subject line again. He asked about arrays, not pointers.

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

                email: rjh at the above domain, - www.

                Comment

                Working...