how to deal with arrays with unknown length?

Discussion in 'C++' started by Magcialking, Aug 30, 2006.

  1. Magcialking

    Magcialking Guest

    for example,in the function "int a(int[] b)", I wanna every element of
    array b to be dealt with, but b's length remains unkown, so what can I
    do?
     
    Magcialking, Aug 30, 2006
    #1
    1. Advertising

  2. Magcialking

    David Harmon Guest

    On 29 Aug 2006 20:24:29 -0700 in comp.lang.c++, "Magcialking"
    <> wrote,
    >for example,in the function "int a(int[] b)", I wanna every element of
    >array b to be dealt with, but b's length remains unkown, so what can I
    >do?


    Write it in C++ instead of C.
    Avoid bare naked arrays.
    You might end up with something like:
    int a(std::vector<int> & b)

    Otherwise, you have to pass the number of elements as an additional
    argument, or something.
     
    David Harmon, Aug 30, 2006
    #2
    1. Advertising

  3. Magcialking

    red floyd Guest

    David Harmon wrote:
    > On 29 Aug 2006 20:24:29 -0700 in comp.lang.c++, "Magcialking"
    > <> wrote,
    >> for example,in the function "int a(int[] b)", I wanna every element of
    >> array b to be dealt with, but b's length remains unkown, so what can I
    >> do?

    >
    > Write it in C++ instead of C.
    > Avoid bare naked arrays.
    > You might end up with something like:
    > int a(std::vector<int> & b)
    >
    > Otherwise, you have to pass the number of elements as an additional
    > argument, or something.
    >


    Not to mention writing in C++ instead of Java.
     
    red floyd, Aug 30, 2006
    #3
  4. "Magcialking" <> schrieb im Newsbeitrag
    news:...
    > for example,in the function "int a(int[] b)", I wanna every element
    > of
    > array b to be dealt with, but b's length remains unkown, so what can
    > I
    > do?


    In C you would pass the number of elements in a 2nd argument. You can
    also define a "last item" value. Or you can simply guess ;)
     
    Gernot Frisch, Aug 30, 2006
    #4
  5. Magcialking

    Guest

    use VECTOR ,they are meant to deal with situations of growing array or
    in cases when you are not sure of the array size
     
    , Aug 30, 2006
    #5
  6. Magcialking posted:

    > for example,in the function "int a(int[] b)", I wanna every element of
    > array b to be dealt with, but b's length remains unkown, so what can I
    > do?


    The following two functions are exactly equivalent:

    void Func(int arr[]) {}

    void Func(int *arr) {}

    You cannot pass an array by value to a function. If you write a function
    signature with a parameter such as arr[], you're really just taking a
    pointer by value.

    If you want your function to be able to take an array (and to know its
    length), then start off with a function such as the following:


    void Func_BehindTheCurtains(int *p,size_t len) {}

    , and then invoke it using the following:

    template<size_t len>
    void Func(int (&arr)[len])
    {
    return Func_BehindTheCurtains(arr,len);
    }

    Now you can invoke it as follows:

    int main()
    {
    int arr[43];

    Func(arr);
    }

    --

    Frederick Gotham
     
    Frederick Gotham, Aug 30, 2006
    #6
  7. Frederick Gotham wrote:
    > Magcialking posted:
    >
    >> for example,in the function "int a(int[] b)", I wanna every element
    >> of array b to be dealt with, but b's length remains unkown, so what
    >> can I do?

    >
    > The following two functions are exactly equivalent:
    >
    > void Func(int arr[]) {}
    >
    > void Func(int *arr) {}
    >
    > You cannot pass an array by value to a function. If you write a
    > function signature with a parameter such as arr[], you're really just
    > taking a pointer by value.
    >
    > If you want your function to be able to take an array (and to know its
    > length), then start off with a function such as the following:
    >
    >
    > void Func_BehindTheCurtains(int *p,size_t len) {}
    >
    > , and then invoke it using the following:
    >
    > template<size_t len>
    > void Func(int (&arr)[len])
    > {
    > return Func_BehindTheCurtains(arr,len);
    > }
    >
    > Now you can invoke it as follows:
    >
    > int main()
    > {
    > int arr[43];
    >
    > Func(arr);
    > }


    There is really no need for the intervening template. In this case, just
    look up in the function where 'arr' is declared, learn about 43, and then
    just call

    Func_BehindTheCurtains(arr, 43);

    which is what the optimizing compiler will probably do anyway.

    The point is that when you are in the context when you have 'arr' that
    has come to you as a pointer, and you have no idea how the array behind it
    (if any) was defined, the template trick is not going to help. You _need_
    to know the size. So, nothing beats pulling the size in as an argument.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 30, 2006
    #7
  8. Victor Bazarov posted:

    > There is really no need for the intervening template.



    I put in an intervening template (rather than making the _actual_ function
    a template) so that there would be only one _actual_ function for all array
    lengths, and only one set of static data, e.g.:

    void Func(int *p,size_t len)
    {
    int static blah = 5; /* Only one static object */
    }


    > In this case, just look up in the function where 'arr' is declared,
    > learn about 43, and then just call
    >
    > Func_BehindTheCurtains(arr, 43);
    >
    > which is what the optimizing compiler will probably do anyway.



    I thought the OP's aim was to be able to simpye specify:

    Func(arr);

    This is good for two reasons:

    (1) It's prettier than having to specify the length.
    (2) It won't break if the length is changed.


    > The point is that when you are in the context when you have 'arr' that
    > has come to you as a pointer, and you have no idea how the array behind
    > it (if any) was defined, the template trick is not going to help. You
    > _need_ to know the size. So, nothing beats pulling the size in as an
    > argument.



    Yes, size must be pulled in as an argument, but it might be handy to get a
    template to do it for you (or even something like:)

    #define Func(arr) Func_BehindTheCurtains((arr),sizeof(arr)/sizeof*(arr))

    --

    Frederick Gotham
     
    Frederick Gotham, Aug 30, 2006
    #8
  9. Frederick Gotham wrote:
    > [..]
    > Yes, size must be pulled in as an argument, but it might be handy to
    > get a template to do it for you (or even something like:)
    >
    > #define Func(arr)
    > Func_BehindTheCurtains((arr),sizeof(arr)/sizeof*(arr))


    You cannot "get a template to do it for you" if you are already in
    a context that has no size. You _have_to_ change all the functions
    to pull the size as an argument.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 30, 2006
    #9
  10. Magcialking

    Guest

    The problem with passing the number of elements is that it is prone to
    errors. Someone passes in the wrong value and you corrupt memory.
    ------------------------------------------------
    Bumperstickers: http://www.cafepress.com/bush_doggers?pid=2794571

    Gernot Frisch wrote:
    > "Magcialking" <> schrieb im Newsbeitrag
    > news:...
    > > for example,in the function "int a(int[] b)", I wanna every element
    > > of
    > > array b to be dealt with, but b's length remains unkown, so what can
    > > I
    > > do?

    >
    > In C you would pass the number of elements in a 2nd argument. You can
    > also define a "last item" value. Or you can simply guess ;)
     
    , Aug 30, 2006
    #10
  11. > void Func_BehindTheCurtains(int *p,size_t len) {}
    >
    > , and then invoke it using the following:
    >
    > template<size_t len>
    > void Func(int (&arr)[len])
    > {
    > return Func_BehindTheCurtains(arr,len);
    > }



    int* pA = new int[128];
    Func(pA);

    ???
     
    Gernot Frisch, Aug 31, 2006
    #11
  12. Gernot Frisch posted:

    > int* pA = new int[128];
    > Func(pA);
    >
    > ???



    int (&arr)[128] = *new int[1][128];
    Func(arr);

    If the array length isn't a compile time constant, then you can't use the
    template.

    --

    Frederick Gotham
     
    Frederick Gotham, Aug 31, 2006
    #12
  13. "Frederick Gotham" <> schrieb im Newsbeitrag
    news:SrAJg.13305$...
    > Gernot Frisch posted:
    >
    >> int* pA = new int[128];
    >> Func(pA);
    >>
    >> ???

    >
    >
    > int (&arr)[128] = *new int[1][128];
    > Func(arr);
    >
    > If the array length isn't a compile time constant, then you can't
    > use the
    > template.


    That's what I wanted to point out.
     
    Gernot Frisch, Aug 31, 2006
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    869
    info_
    Mar 28, 2005
  2. M. Clift
    Replies:
    8
    Views:
    435
    M. Clift
    Aug 19, 2004
  3. rbt

    deal or no deal

    rbt, Dec 22, 2005, in forum: Python
    Replies:
    7
    Views:
    597
    Duncan Smith
    Dec 28, 2005
  4. Philipp
    Replies:
    21
    Views:
    1,183
    Philipp
    Jan 20, 2009
  5. Vincent Arnoux
    Replies:
    1
    Views:
    271
    Arnaud Bergeron
    Aug 11, 2006
Loading...

Share This Page