Thread: Getting the length of an array

1. Getting the length of an array

Dear friends.
When I allocate memory for an array, can I afterwards get an information about the size of the allocated memory?
The background is a simple function named vectorsum, that I programmed.
That is:
Code:
```vectorsum(float* vector,int length)
{
int i=0;
float    sum=0;
for(i=0;i<length;i++)
sum+=vector;
return sum;
}```
I find it a bit stupid, that I have to give the length to the function and it is also bad, that it is restricted to one data type (float*).

Pharao

2. When I allocate memory for an array, can I afterwards get an information about the size of the allocated memory?
Generally, no. You have to keep track of the array size, and/or use some specially designated value as a terminator (e.g., the approach with null terminated strings). One exception is with sizeof on an array (but not a pointer to somewhere in the array), where you can compute the number of elements by dividing that by sizeof an element of the array... but then you often already know the size in such a case.

3. If you are using a custom memory manager you can typically get the size of a given dynamic block of memory from the memory header. Reading the headers used by malloc() would not necessarily be a very portable solution, however since different malloc()s are designed different ways with entirely different types of headers.

4. If you are using a custom memory manager you can typically get the size of a given dynamic block of memory from the memory header. Reading the headers used by malloc() would not necessarily be a very portable solution, however since different malloc()s are designed different ways with entirely different types of headers.
Well, yes, the implementation has to know how much memory you've allocated, if only to copy it to a new place if necessary during a realloc() call. I wouldn't suggest going looking for this value yourself, though.

I find it a bit stupid, that I have to give the length to the function
That's what you have to do in C. (BTW: it's a good idea to use size_t when you're dealing with the number of elements in an array, since that's what type sizeof evaluates to.)

and it is also bad, that it is restricted to one data type (float*).
Well, in C++ you could use templates, but in C you basically have to live with that too. Unless you want to use a macro or something, but that's messy. Don't worry about it too much -- even the standard C library has cos() for doubles, cosf() for floats, and cosl() for long doubles. http://www.opengroup.org/onlinepubs/...tions/cos.html

5. Originally Posted by Pharao
I find it a bit stupid, that I have to give the length to the function and it is also bad, that it is restricted to one data type (float*).
You picked the wrong language, friend!
But if you want something close to hardware, efficient and such (built on C), and more flexibility, I do echo the suggestion to look into C++.
(Yes, it can solve both your problems.)

6. > Unless you want to use a macro or something, but that's messy.

Not too messy for C99; see tgmath.h.

7. @Elysia: how can it solve the problem of having to pass the size of the array to the function? The only way I can see around that is to create a class (or use an existing class like std::vector), but you can make structs in C as well.

Not too messy for C99; see tgmath.h.
Yes, I suppose so. But if you can figure out a way to declare a sum variable of the same type as a macro's parameter, and have the macro evaluate to that value, let me know.

The way I see it, you'd probably have to pass "float" or "double" to the macro, which makes it no better than creating separate functions with different names. Of course, there may be another way to do it, because presumably tgmath.h does.

But still, templates would be so much simpler in this case.

8. Originally Posted by dwks
@Elysia: how can it solve the problem of having to pass the size of the array to the function? The only way I can see around that is to create a class (or use an existing class like std::vector), but you can make structs in C as well.
Well, that was the point. Using std::vector or std::string.
Because the original need was not to have to keep track of the size yourself, no?

Plus macros are hard to debug, no? They weren't really made very well for long functions of code.

9. Well, first of all you keep giving the OP C++ specific solutions to his C project. Which doesn't help them a whole lot in the grand scheme of things. And what further makes the point mood is that if the OP were using C++ I would simply tell them to use an STL vector anyway. As would you.

10. The point is that C can't do all the things the OP wants. It wasn't built for it. C++ is and was therefore given as a suggestion not only by me, but others as well.
We have provided the facts, and it's up to the OP to decide what to do with that information.

11. I was actually speaking more directly to dwk, Elysia. You just kind of snuck a post in while I was writing mine. I agree with what you said 100&#37;. Indeed I would go the C++ route on this one. But I try not to assume the OP knows how to program in C or C++ with any level of confidence until I observe otherwise.

12. Because the original need was not to have to keep track of the size yourself, no?
No, the original problem was that the size needed to be passed to the function. There's no way around this. Either you pass in the size somehow, as a separate parameter or as part of a class or struct, or through some obscure method like global variables. Or you hard-code it. Basically, there's no way around it, C or C++ nonwithstanding.

 Speaking of snuck-in posts -- two happened to me!

As for me giving the OP C++-specific solutions . . . I already said "not to worry about it too much". People, even the designers of the original C89 standard library, accepted the necessity of having to create different functions for different variable types.

But since the OP was looking for ways around this, I supplied the only two I could think of. Macros, and templates. Neither of which is much use here.

So -- don't worry about it too much. Make different versions if you have to. [/edit]

13. Originally Posted by master5001
I was actually speaking more directly to dwk, Elysia. You just kind of snuck a post in while I was writing mine...
Oh! Right, sorry about that :)

Originally Posted by dwks
No, the original problem was that the size needed to be passed to the function. There's no way around this. Either you pass in the size somehow, as a separate parameter or as part of a class or struct, or through some obscure method like global variables. Or you hard-code it. Basically, there's no way around it, C or C++ nonwithstanding.
While I don't agree, I won't further argue it ;)

14. Originally Posted by Elysia
While I don't agree, I won't further argue it ;)
Ditto.

15. You guys type too fast for me.

I guess I'll just finish off by mentioning this . . . if you know that the argument you're passing is in fact an array (and not a pointer), you can use something like this.
Code:
```#define vectorsum_array(vector) \
vectorsum(vector, sizeof vector / sizeof *vector)```