Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to return a correct array size from a function?

Status
Not open for further replies.

sedawk

Programmer
Joined
Feb 5, 2002
Messages
247
Location
US
I have an array and want to detect the size of the array. If I do this, it will return the correct length:

main()
{
int a = { 1, 2, 3};
int len = sizeof a / sizeof a[0];
// return correct length = 3
cout << &quot;the correct length is &quot; << len << endl;
}

But if put it in this way, it doesn't work:
int length(*v);
// or
// int length(v[]);
main()
{
int a = { 1, 2, 3};
int len = length(a);
// it only returns length = 1
cout << &quot;the vector length is &quot; << len << endl;
}

int length(*v)
{
return sizeof v / sizeof v[0];
}
// end code

I know the reason is in the function length(*v) it only use the first element of v (since v is a pointer now) to do the size calculation. That is why it returns 1. But what is the right way to return a array size from a function?
 
the answer is what you can not do it. There are too fiew cases when you can do it. So, better is you to incapsulate array in a class, save number of elements in a variable (for example size) and size of array in another variable (for example size_in_memory)

Ion Filipski
1c.bmp
 
No way to return (or detect, or calculate) array size if your array is function parameter.
In C and C++ expressions array name automatically converted in a pointer to a basic array type.
Code:
int a[NNN];
...
f(a); // int* passed to f(), pointer to a[0] value.
...
int f(int a[]) // we have int* pointer to start of some memory area.
{
return sizeof a; // size of pointer to int returned!
}
The only exception from the rule is
Code:
sizeof
op in the array declaration scope[/code]. In that case
Code:
sizeof array_name
returns full array size.

But in function body
Code:
int a[4]
parameter IS NOT an array declaration! In the body
Code:
sizeof a
always returns pointer to int sizeof. Your approach is fruitless.

You may declare your own array class with more friendly properties or you may use STL containers. But don't try to get round a difficulties (and advantages) of the old C array heritage...
 
Thanks. I am clear now: a scope issue. So a class is almost always global except private part. Then I can access the size of the array or STL vector.
 
sedawk, be careful: the point is not that a class is almost always global! Array wrapper class must have true array size as one of its member (see IonFilipski's post above). Essentially this class must be array descriptor (as in some other languages - PL/I, for example). But now you may use STL valarray template for that purpose, for example - why not?

If your problem area critically depends on array handling, valarray is (probably) the best choice.

C++ inherits its arrays from C. In original C problem area (system programming) they could pass (as usually, 1D) array size as parameter - it was enough...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top