468,457 Members | 1,628 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

how to deal with arrays with unknown length?

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?

Aug 30 '06 #1
12 8517
On 29 Aug 2006 20:24:29 -0700 in comp.lang.c++, "Magcialking"
<ma********@163.comwrote,
>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.

Aug 30 '06 #2
David Harmon wrote:
On 29 Aug 2006 20:24:29 -0700 in comp.lang.c++, "Magcialking"
<ma********@163.comwrote,
>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.
Aug 30 '06 #3

"Magcialking" <ma********@163.comschrieb im Newsbeitrag
news:11*********************@p79g2000cwp.googlegro 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 ;)
Aug 30 '06 #4
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 '06 #5
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
Aug 30 '06 #6
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
Aug 30 '06 #7
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
Aug 30 '06 #8
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
Aug 30 '06 #9
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" <ma********@163.comschrieb im Newsbeitrag
news:11*********************@p79g2000cwp.googlegro 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 ;)
Aug 30 '06 #10
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);

???
Aug 31 '06 #11
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
Aug 31 '06 #12

"Frederick Gotham" <fg*******@SPAM.comschrieb im Newsbeitrag
news:Sr*******************@news.indigo.ie...
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.
Aug 31 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by M. Clift | last post: by
11 posts views Thread by TomServo | last post: by
4 posts views Thread by Anthra Norell | last post: by
23 posts views Thread by Himanshu Chauhan | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.