while(array) i++;
but that is obviously wrong. (It worked with strings so I figured "oh, what the hell?"). When I create an array inside the same function I can find the size using sizeof(array) / sizeof(char) but I already know the size in that situation.
Finding array length -- C++
Do I HAVE to use a vector? I prefer to use them only when it is necessary.
Is there a way to find the length of an array that is passed to a function? I thought I could use something like
Quote:Original post by skulldrudgery
EDIT: How do they find the length with a vector? Just wondering.
They don't "find" it. They keep track of it. A typical vector implementation would keep a pointer to the beginning of the vector, to the end of the vector (one past the last element) and to the end of the allocated block (one past the last valid address in the block).
The length of the vector would then simply be pEnd - pBegin, and its capacity pEndBlock - pBegin.
So...In other words, shut my uppity n00b mouth and just use the stinking vector.[grin]
Thanks for the info.
Thanks for the info.
#include <vector>int main(){vector<char>Size;Size.push_back('a'); //Size[0] = 'a'Size.push_back('b'); //Size[1] = 'b'Size.push_back('c'); //Size[2] = 'c'int SizesSize = Size.size();//3}
Quote:Original post by skulldrudgery
Do I HAVE to use a vector?
No, you can use a list, a deque, a struct { type_t *data; size_t size; }, void foo(size_t size, type_t *data), etc.
Quote:
while(array) i++;
Step back for a second. Why does this work for strings and not for other arrays? (Hint: It doesn't work for char arrays. What's the difference between a char array and a string?) Once you figure that out, it may be possible to adapt that solution to your needs.
Quote:
When I create an array inside the same function I can find the size using sizeof(array) / sizeof(char)
Note that this only works for char arrays and, for a string, will give a different answer from strlen(array). A more robust version is (sizeof array / sizeof *array).
Quote:
but I already know the size in that situation
The (sizeof array / sizeof *array) solution has the benefit of only changing the value in one place.
But, really, why do you use vector only when necessary? You should use it not only because it solves your problem, not only because it solves other problems, not only because it's at least as clean as any solution you will come up with, not only because it will probably have no more overhead than your solution, but because it's the standard C++ way to handle the situation ("vector" is how a C++ programmer typically spells "array", and u reedooss reedubilittee bie uzing ur p0wn spelling).
Quote:Original post by Anonymous PosterQuote:
while(array) i++;
Step back for a second. Why does this work for strings and not for other arrays? (Hint: It doesn't work for char arrays. What's the difference between a char array and a string?) Once you figure that out, it may be possible to adapt that solution to your needs.
It should work on a null-terminated char array.
Quote:Original post by Anonymous Poster
Note that this only works for char arrays and, for a string, will give a different answer from strlen(array). A more robust version is (sizeof array / sizeof *array).
I must voice my disagreement here. (sizeof array / sizeof *array), just like (sizeof array / sizeof char) both suffer from the fatal flaw that they will silently return an erroneous result if array isn't an array name, but a pointer. And given how 'easy' it is for an array to decay to a pointer...
In C++, a safe solution is the following template function:
template<class T, size_t N> size_t N dimension_of(T (&)[N]) { return N; }
or, if you need a version that provides a result at compile-time:
template<size_t N> struct dimension_of_hs { char dummy[N]; };template<class T, size_t N> dimension_of_hs<N> dimension_of_hfn(T (&)[N]);#define dimension_of(X) (sizeof dimension_of_hfn(X))
Both versions will cause a compiler error if you try to pass them pointers.
Usage: dimension_of(array)
Result: The number of elements of the array.
Quote:Original post by Frunytemplate<size_t N> struct dimension_of_hs { char dummy[N]; };template<class T, size_t N> dimension_of_hs<N> dimension_of_hfn(T (&)[N]);#define dimension_of(X) (sizeof dimension_of_hfn(X))
Both versions will cause a compiler error if you try to pass them pointers.
Usage: dimension_of(array)
Result: The number of elements of the array.
Actually, technically that wouldn't necessarily work because of padding (the struct size isn't guaranteed to be the size of the array). Instead of returning the array encapsulated in a struct, have the return-type be a reference to an array of char:
template<typename T, ::std::size_t N> char (&dimension_of_hfn(T (&)[N]))[N];
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement