Velocity Reviews > Safely working out how many rows in an array?

# Safely working out how many rows in an array?

pkirk25
Guest
Posts: n/a

 09-26-2006
Assume an array of structs that is having rows added at random. By the
time it reaches your function, you have no idea if it has a few hundred
over over 10000 rows.

When your function recieves this array as an argument, is there a safe
way to establish how many rows that are or should I iterate over a
field I know will always be used and use the final value of the
iterator as the value of the array?

Richard Heathfield
Guest
Posts: n/a

 09-26-2006
pkirk25 said:

> Assume an array of structs that is having rows added at random. By the
> time it reaches your function, you have no idea if it has a few hundred
> over over 10000 rows.
>
> When your function recieves this array as an argument, is there a safe
> way to establish how many rows that are or should I iterate over a
> field I know will always be used and use the final value of the
> iterator as the value of the array?

The trick is to keep track of how many elements the array has, and to pass
that information to functions that need it. Objects are relatively cheap.
Something as small as a size_t is practically free! So don't be squeamish

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Keith Thompson
Guest
Posts: n/a

 09-26-2006
"pkirk25" <(E-Mail Removed)> writes:
> Assume an array of structs that is having rows added at random. By the
> time it reaches your function, you have no idea if it has a few hundred
> over over 10000 rows.
>
> When your function recieves this array as an argument, is there a safe
> way to establish how many rows that are or should I iterate over a
> field I know will always be used and use the final value of the
> iterator as the value of the array?

You need to rethink your question.

It's not possible to pass an array as an argument in C. What you can
do is, for example, pass the address of (equivalently: a pointer to)
an array's first element as an argument. This pointer can then be
used to access the elements of the array, but it doesn't tell you how
many there are.

The language does a few things that seemingly conspire to make it
*look* like you're passing the array itself, but you're really not.

As Richard Heathfield wrote, the most straightforward way to do this
is to keep track of the size yourself and pass it to your function as
an extra argument.

Other solutions exist.

You should also read section 6 of the comp.lang.c FAQ, available at
<http://www.c-faq.com>.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

pkirk25
Guest
Posts: n/a

 09-26-2006

Richard Heathfield wrote:
[snip]
>
> The trick is to keep track of how many elements the array has, and to pass
> that information to functions that need it. Objects are relatively cheap.
> Something as small as a size_t is practically free! So don't be squeamish

[snip]

A file has over 300k rows of which less than 100 are likely to be of
interest.

int ScanFile(const FILE *srcFile,
struct realm_structure *rlm,
int *i)
{
/*
1. do something very clever to find a match
2. update the struct so its a useful matrix
3. i++
*/
return 0;
}

Is this what you were thinking?

If i get the iteration of the count wrong in any 1 function, I could
end up confused. But as a way of dealing with the problem, it does
look very good.

Michael Mair
Guest
Posts: n/a

 09-26-2006
Richard Heathfield wrote:
> pkirk25 said:
>
>>Assume an array of structs that is having rows added at random. By the
>>time it reaches your function, you have no idea if it has a few hundred
>>over over 10000 rows.
>>
>>When your function recieves this array as an argument, is there a safe
>>way to establish how many rows that are or should I iterate over a
>>field I know will always be used and use the final value of the
>>iterator as the value of the array?

>
> The trick is to keep track of how many elements the array has, and to pass
> that information to functions that need it. Objects are relatively cheap.
> Something as small as a size_t is practically free! So don't be squeamish

Indeed.

@OP:
An alternative not using less but more space is using a data structure
different from an array if it is more suitable to your problem. Arrays
do not grow rows magically, so you have to deal with reallocation,
keeping track of the number of rows etc.
If you, for example, use linked lists, then insertion of new rows
is comparatively cheap and iteration costs (but for a constant factor)
the same as for an array.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.

William Ahern
Guest
Posts: n/a

 09-26-2006
On Tue, 26 Sep 2006 23:06:17 +0000, Richard Heathfield wrote:

> pkirk25 said:
>
>> Assume an array of structs that is having rows added at random. By the
>> time it reaches your function, you have no idea if it has a few hundred
>> over over 10000 rows.
>>
>> When your function recieves this array as an argument, is there a safe
>> way to establish how many rows that are or should I iterate over a field
>> I know will always be used and use the final value of the iterator as
>> the value of the array?

>
> The trick is to keep track of how many elements the array has, and to pass
> that information to functions that need it. Objects are relatively cheap.
> Something as small as a size_t is practically free! So don't be squeamish

Put another way, don't ever discard information. Ideally, such information
is kept from the very beginning, and not derived at some intermediate
point.

Andrea Laforgia
Guest
Posts: n/a

 09-26-2006
On 26 Sep 2006 16:02:31 -0700, "pkirk25" <(E-Mail Removed)> wrote:

>When your function recieves this array as an argument, is there a safe
>way to establish how many rows that are [...]

Usually, you pass the "dimension" of the array to your function.
A prototype of that function could be the following:

void yourFunction(int rows[], int rowCount);

Alternatively, you may want to use a "sentinel value".

An example of a an array terminated by a sentinel value is argv, one
of the two parameters of the function main():

int main(int argc, char *argv[])

The standard prescribes that argv[argc] shall be a null pointer, so
the following code is perfectly legal:

#include <stdio.h>

int main(int argc, char *argv[])
{
int count;
for (count=0; argv[count]; count++)
;
printf("argc=%d\n", argc, count);
return 0;
}

Andrea Laforgia
Guest
Posts: n/a

 09-26-2006
On Wed, 27 Sep 2006 01:27:28 +0200, Andrea Laforgia
<(E-Mail Removed)> wrote:

> printf("argc=%d\n", argc, count);

Of course, it is:

> printf("argc=%d\n", count);

I would demonstrate that argc == count.

Andrew Poelstra
Guest
Posts: n/a

 09-27-2006
On Tue, 2006-26-09 at 16:02 -0700, pkirk25 wrote:
> Assume an array of structs that is having rows added at random. By the
> time it reaches your function, you have no idea if it has a few hundred
> over over 10000 rows.
>
> When your function recieves this array as an argument, is there a safe
> way to establish how many rows that are or should I iterate over a
> field I know will always be used and use the final value of the
> iterator as the value of the array?
>

I'm not sure if I understand your question, but the usual method for
doing this is to pass the length of the array into the function:

int my_function (char array[], size_t len);

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Keith Thompson
Guest
Posts: n/a

 09-27-2006
Andrew Poelstra <(E-Mail Removed)> writes:
> On Tue, 2006-26-09 at 16:02 -0700, pkirk25 wrote:
>> Assume an array of structs that is having rows added at random. By the
>> time it reaches your function, you have no idea if it has a few hundred
>> over over 10000 rows.
>>
>> When your function recieves this array as an argument, is there a safe
>> way to establish how many rows that are or should I iterate over a
>> field I know will always be used and use the final value of the
>> iterator as the value of the array?
>>

>
> I'm not sure if I understand your question, but the usual method for
> doing this is to pass the length of the array into the function:
>
> int my_function (char array[], size_t len);

And keep in mind that, in a parameter declaration (and *only* in a
parameter declaration), "char array[]" really means "char *array".

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.