Finding array length - with a pointer

Started by
11 comments, last by Zahlman 17 years, 7 months ago
Hey, In a project I'm doing I have a method that takes quite a lot of arguments so in order to make things a bit smaller I decided to cut out unnecessary arguments. One of these arguments was the amount of elements in an array. The array itself is passed by reference to the method - that is, the method takes a pointer to the array as argument. I decided to cut off the 'numElements' argument because I figured this would also work:

/* Original function (simplified, just to show the problem) */
void SomeMethod ( HWND * hwnd, int numArgs )
{
    for ( int i = 0; i < numArgs; i++ )
    {
        DoSomethingWithWindow ( (*hwnd) );
    }
}

/* Function with less arguments */
void SomeMethod ( HWND * hwnd )
{
    int numArgs = sizeof(hwnd)/sizeof(HWND);
    for ( int i = 0; i < numArgs; i++ )
    {
        DoSomethingWithWindow ( (*hwnd) );
    }
}

Of course, this failed in many ways because 'sizeof(hwnd)' returns only the size of the pointer, not the array to which it points... Is it possible to leave the numArgs argument out of the parameter list of said function and still calculate the correct number of elements by only using the pointer to the first element of the array?
Advertisement
No, it is not possible. It would work with an std::vector<HWND>, though.
Simple answer is you can't. Unless you pass in the array length, or determine it at runtime, your SOL. You could do the following extremely unsafe code to accomplish what I think you want

void SomeMethod( HWND * hwnd)
{
int curHwnd = 0;
while(hwnd[curHwnd] != null)
{
DoSomethingWithWindow((*hwnd));
curHwnd++;
}

This will give you the total count of HWNDs in the array ( in the value curHwnd ). With two caveats. First off, you probrably just as well off passing in the length. Second, you should make sure the hwnd array has been memset to 0 when created or all kinds of neat things can happen.
Thanks for your replies, I think I'll just go with the extra argument and see if I can cut some others off. I'd hate to think of what'd happen when someone else uses that method and forgets to ZeroMemory or memset the HWND array to 0... [grin]
I'm going to go with ToohrVyk on this one and recommend a std::vector (assuming c++). This will not only solve your problem but save headaches in the future.
Well yeah, I could do that, but I think it's a bit of an overkill for the purpose that it serves. A simple array suits me fine here, it was only a question of including the number of elements or not...
Quote:Original post by rogierpennink
Hey,

In a project I'm doing I have a method that takes quite a lot of arguments so in order to make things a bit smaller I decided to cut out unnecessary arguments. One of these arguments was the amount of elements in an array. The array itself is passed by reference to the method - that is, the method takes a pointer to the array as argument.

I decided to cut off the 'numElements' argument because I figured this would also work:

*** Source Snippet Removed ***

Of course, this failed in many ways because 'sizeof(hwnd)' returns only the size of the pointer, not the array to which it points...
Is it possible to leave the numArgs argument out of the parameter list of said function and still calculate the correct number of elements by only using the pointer to the first element of the array?


For static arrays:
template<class T, size_t N>size_t size(T[N]) { return N; }

The above is only for static arrays, (i.e. Foo foo[12];) This will only work with direct static arrays, not with pointers to static arrays.

For dynamic arrays:
std::vector<Element> elements;size_t size = elements.size();

Really, there isn't much of an excuse for not using vectors for dynamic arrays (assuming you aren't doing something too funky with pointers).
When you discover how many other places around the system you have to pass an array and a length count around "together", you'll probably come to rethink that :)

Use std::vector or list, it will save you from much of headache. And believe me, it isn't overkill. It is just code that somebody made and tested pretty well.

The amount of code needed to use std::vector is pretty much the same as you'd need for an array anyway.

At least, if you don't want to mess with std, write your own array wrapper template with some bound checking.

Cheers !
Thanks for your reply agi_shi. As you said, there is not much of an excuse for not using vectors if the array is dynamic. In my case the array isn't dynamic however. It's a global array that contains the windowhandles of each window that my future game engine should render to. As such, the number of elements is only needed for the creation of a swapchain. This use is so trivial that I didn't bother with std::vector ;)

This topic is closed to new replies.

Advertisement