Jump to content
  • Advertisement
Sign in to follow this  
rogierpennink

Finding array length - with a pointer

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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).

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!