473,320 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 9552
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: M. Clift | last post by:
Hi All, I have a list of varying length. Would someone know the way to get the last two values for this? I can see how this is done with a list that I know the length of, but not one thats...
3
by: James | last post by:
Hello, We've been given a program that reads data from a .dat file. It looks something like this: 1 1.0 1.2 2 2.4 3.5 3 3.4 6.5 4 3.3 2.2 ....and so on. I need to know a way of preinting...
11
by: TomServo | last post by:
I am writing code that needs to run on a variety of Unix systems. I am calling the statvfs and statfs system calls and I need to to convert some of the integers returned to character strings....
4
by: Anthra Norell | last post by:
Hi, I keep working around a little problem with unpacking in cases in which I don't know how many elements I get. Consider this: def tabulate_lists (*arbitray_number_of_lists): table = zip...
1
by: SoFaraway | last post by:
Hi all, In C, to read a line from a file, we need to allocate some fixed length of memory first. However, if the line is longer than the length of the allocated memory, it can't be read correctly....
23
by: Himanshu Chauhan | last post by:
Hi! I was wondering, In the first parse of a singly linked list of unknown length, is it possible to know when we are at middle of the linked list? Regards --Himanshu
3
by: RobbSadler | last post by:
This post was meant to go at the end of another post named "How to Compare Two Byte Arrays" which was answered by Vadym Stetsyak, but it was closed. I used his code from that post to create this, and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.