how to deal with arrays with unknown length?



 DEVELOP > c-Plus-Plus > how to deal with arrays with unknown length?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Magcialking"
Date: 29 Aug 2006 10:24:29 PM
Object: 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?
.

User: "Gernot Frisch"

Title: Re: how to deal with arrays with unknown length? 30 Aug 2006 04:31:17 AM
"Magcialking" <magic_king@163.com> schrieb im Newsbeitrag
news:1156908269.372396.60840@p79g2000cwp.googlegroups.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 ;)
.
User: ""

Title: Re: how to deal with arrays with unknown length? 30 Aug 2006 11:13:01 AM
use VECTOR ,they are meant to deal with situations of growing array or
in cases when you are not sure of the array size
.

User: ""

Title: Re: how to deal with arrays with unknown length? 30 Aug 2006 03:02:05 PM
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" <magic_king@163.com> schrieb im Newsbeitrag
news:1156908269.372396.60840@p79g2000cwp.googlegroups.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 ;)

.


User: "Frederick Gotham"

Title: Re: how to deal with arrays with unknown length? 30 Aug 2006 12:17:48 PM
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
.
User: "Gernot Frisch"

Title: Re: how to deal with arrays with unknown length? 31 Aug 2006 04:04:14 AM

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);
???
.
User: "Frederick Gotham"

Title: Re: how to deal with arrays with unknown length? 31 Aug 2006 07:17:54 AM
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
.
User: "Gernot Frisch"

Title: Re: how to deal with arrays with unknown length? 31 Aug 2006 09:47:42 AM
"Frederick Gotham" <fgothamNO@SPAM.com> schrieb im Newsbeitrag
news:SrAJg.13305$j7.326838@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.
.



User: "Victor Bazarov"

Title: Re: how to deal with arrays with unknown length? 30 Aug 2006 12:29:06 PM
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
.
User: "Frederick Gotham"

Title: Re: how to deal with arrays with unknown length? 30 Aug 2006 12:40:29 PM
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
.
User: "Victor Bazarov"

Title: Re: how to deal with arrays with unknown length? 30 Aug 2006 02:16:35 PM
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
.




User: "David Harmon"

Title: Re: how to deal with arrays with unknown length? 29 Aug 2006 11:03:23 PM
On 29 Aug 2006 20:24:29 -0700 in comp.lang.c++, "Magcialking"
<magic_king@163.com> 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.
.
User: "red floyd"

Title: Re: how to deal with arrays with unknown length? 30 Aug 2006 12:22:49 AM
David Harmon wrote:

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



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER