Finding array length -- C++

Started by
24 comments, last by Cornstalks 12 years, 2 months ago
Yeah, I see now. I forgot that even when declaring a parameter in the form 'arr[]' - 'arr' is a pointer. So using sizeof(arr)/sizeof(type) is basically useless if it only works for statically allocated arrays. Could some1 explain:

//CODE
template<class T, size_t N> T decay_array_to_subtype(T (&a)[N]);
#define dimension_of(X) (sizeof(X)/sizeof(decay_array_to_subtype(X)))


How can this give a compile error if X is only a pointer?


Maybe wrapping an array in a new class with a getLength() function could be a good solution?
Reject the basic asumption of civialisation especially the importance of material possessions
Advertisement
Quote:Original post by Cacks
Yeah, I see now. I forgot that even when declaring a parameter in the form 'arr[]' - 'arr' is a pointer. So using sizeof(arr)/sizeof(type) is basically useless if it only works for statically allocated arrays. Could some1 explain:

//CODE
template<class T, size_t N> T decay_array_to_subtype(T (&a)[N]);
#define dimension_of(X) (sizeof(X)/sizeof(decay_array_to_subtype(X)))


How can this give a compile error if X is only a pointer?


Maybe wrapping an array in a new class with a getLength() function could be a good solution?



It gives a compiler error because an array and a pointer are two distinct types.

A pointer is a variable which contains an address of an element of type T (which may or may not exist).

An array is a contiguous block of N elements of type T.

A variable declared to be a T&, that is “reference to type T”, can only be initialized by an object of type T or by an object that can be converted into a T.

There are no available conversions from type T* (pointer to type T) to type T [N] (array of N elements of type T), so the decay_array_to_subtype function cannot compile when called with a pointer.

Quote:Original post by Anonymous Poster
Anyone got one without the "evil" #define? [looksaround]

This works for me.
    template < typename T, size_t N >    size_t elementsof( T const (&a)[N] )    {        return N;    } 
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by JohnBolton
This works for me.
    template < typename T, size_t N >    size_t elementsof( T const (&a)[N] )    {        return N;    } 


int a[10]; int b[elementsof(a)]; will not compile, nor can us use elementsof(a) as a template argument.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
_countof(x) implementation in Visual Studio is very similar to what's been posted on this thread already; from memory it looks like this:

template <typename T, std::size_t N> char (* helper(T(&)[N]))[N];
#define _countof(x) (sizeof (*helper(x)) + 0)


I remember it well because I could never figure out what is that +0 supposed to be doing.

_countof(x) implementation in Visual Studio is very similar to what's been posted on this thread already; from memory it looks like this:

template <typename T, std::size_t N> char (* helper(T(&)[N]))[N];
#define _countof(x) (sizeof (*helper(x)) + 0)


I remember it well because I could never figure out what is that +0 supposed to be doing.

Duuuuude. You just replied to a thread that is almost 7 years old. Welcome to GameDev and all, but necroing is something that the community generally dislikes, FYI. The OP hasn't even logged in since 7 Sep 2009...
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement