Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to deal with arrays with unknown length?

Reply
Thread Tools

how to deal with arrays with unknown length?

 
 
Magcialking
Guest
Posts: n/a
 
      08-30-2006
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?

 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      08-30-2006
On 29 Aug 2006 20:24:29 -0700 in comp.lang.c++, "Magcialking"
<(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      08-30-2006
David Harmon wrote:
> On 29 Aug 2006 20:24:29 -0700 in comp.lang.c++, "Magcialking"
> <(E-Mail Removed)> 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.
 
Reply With Quote
 
Gernot Frisch
Guest
Posts: n/a
 
      08-30-2006

"Magcialking" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) ups.com...
> 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


 
Reply With Quote
 
ckiitd2001@gmail.com
Guest
Posts: n/a
 
      08-30-2006
use VECTOR ,they are meant to deal with situations of growing array or
in cases when you are not sure of the array size

 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      08-30-2006
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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-30-2006
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


 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      08-30-2006
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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-30-2006
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


 
Reply With Quote
 
defendusa2@yahoo.com
Guest
Posts: n/a
 
      08-30-2006
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" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed) ups.com...
> > 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Assign an unknown value to an unknown variable Vincent Arnoux Ruby 1 08-11-2006 06:12 PM
AMD64 or Semperon, deal or no deal? Tad Confused Computer Information 7 04-13-2006 05:43 PM
deal or no deal rbt Python 7 12-28-2005 08:57 PM



Advertisments