Iterating through an array whose size is unknown?

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

    Iterating through an array whose size is unknown?

    I need to send just an array to a function which then needs to go
    through each element in the array. I tried using sizeof(array) /
    sizeof(array[0]) but since the array is passed into the function,
    sizeof(array) is really sizeof(pointer to first element in array) and
    therefore doesn't solve my problem. If I can't calculate the size of
    the array, can I go through the elements without knowing the size and
    somehow test whether I'm off the end of the array?
  • Jack Klein

    #2
    Re: Iterating through an array whose size is unknown?

    On 14 Jul 2004 21:30:03 -0700, matthew_hurne@y ahoo.com (matthurne)
    wrote in comp.lang.c++:
    [color=blue]
    > I need to send just an array to a function which then needs to go
    > through each element in the array. I tried using sizeof(array) /
    > sizeof(array[0]) but since the array is passed into the function,
    > sizeof(array) is really sizeof(pointer to first element in array) and
    > therefore doesn't solve my problem. If I can't calculate the size of
    > the array, can I go through the elements without knowing the size and
    > somehow test whether I'm off the end of the array?[/color]

    But you do know the size of the array, after all you created the
    array.

    There are two possibilities if you insist using an array instead of a
    std::vector:

    1. Pass the size of the array as an additional parameter to the
    function.

    2. If the data in the array permits, have a dummy sentinel value that
    tells the called function that it has reached the end of the array.
    That is how the C library string functions work, a '\0' marks the end
    of the string.

    --
    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

    • John Harrison

      #3
      Re: Iterating through an array whose size is unknown?

      On 14 Jul 2004 21:30:03 -0700, matthurne <matthew_hurne@ yahoo.com> wrote:
      [color=blue]
      > I need to send just an array to a function which then needs to go
      > through each element in the array. I tried using sizeof(array) /
      > sizeof(array[0]) but since the array is passed into the function,
      > sizeof(array) is really sizeof(pointer to first element in array) and
      > therefore doesn't solve my problem. If I can't calculate the size of
      > the array, can I go through the elements without knowing the size and
      > somehow test whether I'm off the end of the array?[/color]

      No you cannot, you have two options

      1) The poor mans option, pass the size of the array into the original
      function, so you can pass it on to the later function.

      2) The proper C++ option, use a vector instead of an array. Vectors carry
      their size with them at all times. In others words vectors do what you are
      hoping that arrays do. They also have lots of other useful features like
      being able to grow dynamically. You haven't really learnt C++ programming
      if you haven't learned about vectors. Read a decent C++ book.

      john

      Comment

      • Siemel Naran

        #4
        Re: Iterating through an array whose size is unknown?

        "Jack Klein" <jackklein@spam cop.net> wrote in message
        [color=blue]
        > 1. Pass the size of the array as an additional parameter to the
        > function.
        >
        > 2. If the data in the array permits, have a dummy sentinel value that
        > tells the called function that it has reached the end of the array.
        > That is how the C library string functions work, a '\0' marks the end
        > of the string.[/color]

        Also argv[argc] is valid and points to NULL.

        int main(int argc, char * * argv) {
        char * * iter = argv;
        while (*iter) ++iter;
        assert(iter-argv == argc); // should be true
        argv[argc]; // ok, returns NULL
        argv[argc+1]; // memory access violation!
        }

        Same holds for the extension char * * env extension many compilers support.

        int main(int argc, char * * argv, char * * env);

        A combination of methods (1) and (2) is to have the first element in the
        array denote the number of elements following. Thus one would have { 3, 7,
        2, 4 } for an array of 3 elements. This first element in the array is like
        a sentinel saying the number of elements in the array. But of course, this
        approach may not always be feasible, but it is something to consider.


        Comment

        • matthurne

          #5
          Re: Iterating through an array whose size is unknown?

          All of your suggestions were helpful, however they aren't a solution
          to my problem. It seems there ISN'T a solution to my problem! See, I
          was attempting to code a solution to an archived TopCoder problem
          question. To answer the question I had to have a method with the
          prototype:

          int sum(string s[])

          So I couldn't pass in the size as another parameter. In addition, the
          problem gave examples of what would be passed in and it was simply
          arrays of strings, without any kind of sentinel value/string to tell
          the size of the array. Therefore, that idea is a no-no. I know if I
          were using this in my own program I would take one of your
          suggestions, but I'm assuming if I were actually answering this
          problem during a TopCoder competition, I would have to use the above
          prototype or I would get the problem wrong.

          So here's my thought...maybe the problem was directed/meant to be
          answered in Java. That's the first language I really learned anything
          in and I know that Strings in Java have a size() function, so there
          goes the problem.

          By the way, I have used vectors, my friend. I would prefer them over
          an array but like I've already said, I was given the above prototype
          for the problem. I'm actually almost finished with Accelerated
          C++...the chapter I just finished describes writing your OWN
          vector-like class. Please don't assume that just because I asked a
          specific question about arrays that means I don't know anything else
          about C++.

          Comment

          • John Harrison

            #6
            Re: Iterating through an array whose size is unknown?

            > Please don't assume that just because I asked a[color=blue]
            > specific question about arrays that means I don't know anything else
            > about C++.[/color]

            OK, point taken. I find it very easy to slip into a slightly sarcastic tone
            when replying on c.l.c++, it's something I shouldn't do. In my defence I'd
            say that most of the time there is helpful advice behind the sarcasm.

            john


            Comment

            • matthurne

              #7
              Re: Iterating through an array whose size is unknown?

              "John Harrison" <john_andronicu s@hotmail.com> wrote in message news:<2lnj6sFf2 4utU1@uni-berlin.de>...[color=blue][color=green]
              > > Please don't assume that just because I asked a
              > > specific question about arrays that means I don't know anything else
              > > about C++.[/color]
              >
              > OK, point taken. I find it very easy to slip into a slightly sarcastic tone
              > when replying on c.l.c++, it's something I shouldn't do. In my defence I'd
              > say that most of the time there is helpful advice behind the sarcasm.
              >
              > john[/color]

              That's ok, I don't actually know TOO much about C++. Just more than
              you assumed. ;-) No sweat though.

              Oh, and I realized after my last post that I meant to say...perhaps
              the problem was written for Java because ARRAYS in Java have the
              length constant. What I actually wrote was that Strings in Java have
              the size() method, which I believe they actually have the length()
              method, but either way it wasn't the strings I was worried about
              anyway (that and strings in C++ have a size() method too, so no
              difference there). Doh. So yeah, this is just me correcting myself.

              Comment

              • Anil Mamede

                #8
                Re: Iterating through an array whose size is unknown?

                Can't you use a global variable to record the size, or at least a global
                file variable?

                Anil Mamede

                matthurne wrote:
                [color=blue]
                > I need to send just an array to a function which then needs to go
                > through each element in the array. I tried using sizeof(array) /
                > sizeof(array[0]) but since the array is passed into the function,
                > sizeof(array) is really sizeof(pointer to first element in array) and
                > therefore doesn't solve my problem. If I can't calculate the size of
                > the array, can I go through the elements without knowing the size and
                > somehow test whether I'm off the end of the array?[/color]

                Comment

                • Karl Heinz Buchegger

                  #9
                  Re: Iterating through an array whose size is unknown?

                  Anil Mamede wrote:[color=blue]
                  >
                  > Can't you use a global variable to record the size, or at least a global
                  > file variable?[/color]

                  1: Please dont't top post.
                  2: The proposed solution is one, that one wants to avoid at all costs.
                  [color=blue]
                  >
                  > Anil Mamede
                  >
                  > matthurne wrote:
                  >[color=green]
                  > > I need to send just an array to a function which then needs to go
                  > > through each element in the array. I tried using sizeof(array) /
                  > > sizeof(array[0]) but since the array is passed into the function,
                  > > sizeof(array) is really sizeof(pointer to first element in array) and
                  > > therefore doesn't solve my problem. If I can't calculate the size of
                  > > the array, can I go through the elements without knowing the size and
                  > > somehow test whether I'm off the end of the array?[/color][/color]


                  --
                  Karl Heinz Buchegger
                  kbuchegg@gascad .at

                  Comment

                  • Siemel Naran

                    #10
                    Re: Iterating through an array whose size is unknown?

                    "Anil Mamede" <amak@mega.ist. utl.pt> wrote in message news:40f7ba0b$0 $1773
                    [color=blue]
                    > Can't you use a global variable to record the size, or at least a global
                    > file variable?[/color]

                    What if there are 2 arrays?

                    Then probably a map<address of array, size of array> would work, though if
                    two threads insert or remove from the array at once, or one inserts or
                    removes and the other just reads, then we have to guard these insertions,
                    removals, and reads with mutex locks -- so that only one thread can do
                    anything with the global map at one time. Some implementations do in fact
                    do it this way, because when you say delete[] array they need to know the
                    number of elements in the array in order to call destructor on each one,
                    then release the memory of the array.

                    Other implementations might prepend the number of elements in the array in
                    the -1 slot. So for an array of length 3 arr[0] = 7, arr[1] = 1, arr[2] =
                    9, the compiler would insert an arr[-1]=3.

                    The global map approach seems to have the advantage that realloc is easier
                    as you can see how much the current array can grow before it collides with
                    the next one.


                    Comment

                    Working...