how to deal with arrays with unknown length?

M

Magcialking

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

David Harmon

On 29 Aug 2006 20:24:29 -0700 in comp.lang.c++, "Magcialking"
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.
 
R

red floyd

David said:
On 29 Aug 2006 20:24:29 -0700 in comp.lang.c++, "Magcialking"
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.
 
G

Gernot Frisch

Magcialking said:
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 ;)
 
C

ckiitd2001

use VECTOR ,they are meant to deal with situations of growing array or
in cases when you are not sure of the array size
 
F

Frederick Gotham

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);
}
 
V

Victor Bazarov

Frederick said:
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
 
F

Frederick Gotham

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

Victor Bazarov

Frederick said:
[..]
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
 
D

defendusa2

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 said:
Magcialking said:
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 ;)
 
G

Gernot Frisch

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

???
 
F

Frederick Gotham

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

Gernot Frisch

Frederick Gotham said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top