Public Group

# Finding array length - with a pointer

This topic is 4510 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
No, it is not possible. It would work with an std::vector<HWND>, though.

##### Share on other sites
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.

##### Share on other sites
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]

##### Share on other sites
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.

##### Share on other sites
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...

##### Share on other sites
Quote:
 Original post by rogierpenninkHey,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).

##### Share on other sites
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 :)

##### Share on other sites

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 !

##### Share on other sites
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 ;)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 15
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
634151
• Total Posts
3015825
×