Jump to content
  • Advertisement
Sign in to follow this  
kinglamus

Problems passing pointer to array of OBJECTS

This topic is 3911 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

Hello, I want to pass a pointer to an array of OBJECT(s) to a function, I can't figure out how to do this... struct OBJECT { ... void moveObjects(OBJECT * myObjects[]); int main() { OBJECT myObjects[256]; moveObjects(&myObjects); ... } Now this doesn't work, I will get a compile error like... ...Loop.cpp(10): error C2664: 'InitGame' : cannot convert parameter 1 from 'OBJECT (*)[256]' to 'OBJECT *[]' Please help a newbie, I have tried many solutions to try and figure this out...

Share this post


Link to post
Share on other sites
Advertisement
You want either:


void moveObjects(OBJECT * myObjects);


or


void moveObjects(OBJECT myObjects[]);


Then when you call,


OBJECT myObjects[256];
moveObjects(myObjects);


Although what you probably should consider is:


void moveObjects(std::vector<OBJECT> &myObjects);

std::vector<OBJECT> myObjects(256);
moveObjects(myObjects);


unless you have a very good reason to prefer a built in array.

Share this post


Link to post
Share on other sites
Quote:
Original post by CableGuy
Anyway try to cast it to (OBJECT**).


Using a C-style cast, in this situation, is the equivalent of "STFU noob Im rite yuor rong": the compiler is right in saying that you're providing the wrong type of object. It's not complaining because of some obscure religious reason, it's complaining because, if it allowed the call, your program would access random memory and crash.

If you insist on doing things this way despite the compiler's advice (by using a C-style cast to shut it up) your program will simply not work. One's first reflex, when encountering a compiler error, should be to understand that error, not to ignore it by shutting the compiler up.

In this particular situation, the problem comes from invalid syntax of the pointer-to-array (there are brackets missing: int (*array)[size]).

void moveObjects(OBJECT (*myObjects)[256]);

OBJECT myObjects[256];
moveObjects(&myObjects);


Arguably, though, one should consider using either a reference to array:
void moveObjects(OBJECT (&myObjects)[256]);

OBJECT myObjects[256];
moveObjects(myObjects);


Or, much better, a vector, as this would allow providing an arbitrary number of objects instead of always 256:
void moveObjects(std::vector<OBJECT> &myObjects);

std::vector<OBJECT> myObjects(256);
moveObjects(myObjects);

Share this post


Link to post
Share on other sites
ToohrVyk you are a truly beautiful person! Using your advice my program is now working perfectly, thank you very much.
(And thanks everyone else for trying at least :P)

Share this post


Link to post
Share on other sites
I really don't get the point of using std::vector when it is not appropriate.

If I have this:

void someFunc(int array[])
{
for (int i=0;i<256;i++)
{
DoStuffOnInt(array);
}
}


Why on earth would I use std::vector? The 'C' in C++ is still there...

Share this post


Link to post
Share on other sites
Quote:
Original post by CableGuy
I really don't get the point of using std::vector when it is not appropriate.

If I have this:
*** Source Snippet Removed ***
Why on earth would I use std::vector? The 'C' in C++ is still there...


You lose out if you insist on using C methods in C++. Why not use C altogether then?

I think that the OP pulled the constant 256 out of... the air. Just like most arbitrary array size constants. Why would we want only 256 objects maximum? Surely it makes sense that if we want only 10 objects, that we are not wasting 246 slots in our array. Perhaps later on the OP will find that more than 256 Objects make sense in the game, so will have to modify code to take this into account (such as your handwritten loop that includes a magic number, not even a named constant!).

std::vector closely models the game in our mind. When you think of a new game, you hardly think to yourself "My awesome game will feature 256 enemies". You will probably think "My awesome game will feature lots of enemies".

Considering arrays require equal effort (or more, if you need a way to mark slots in the array as 'empty'), the question becomes 'Why not use std::vector'.

Can you elaborate why std::vector is inappropriate in this case?

Share this post


Link to post
Share on other sites
Quote:
Original post by CableGuy
I really don't get the point of using std::vector when it is not appropriate.

If I have this:
*** Source Snippet Removed ***
Why on earth would I use std::vector? The 'C' in C++ is still there...


Because, in this instance, you have to trust that the calling code has passed an array with at least 256 elements to avoid an access violation, and if you change the size of the array this function operates on, you have to ensure everything that calls it is updated as well.

If you did:


void someFunc(std::vector<int> &v)
{
for (size_t i=0;i<v.size();i++)
{
DoStuffOnInt(v);
}
}


you are insulated from all of the above problems.

Share this post


Link to post
Share on other sites
Quote:
Original post by CableGuy
I really don't get the point of using std::vector when it is not appropriate.

If I have this:
*** Source Snippet Removed ***
Why on earth would I use std::vector? The 'C' in C++ is still there...
This really calls for a more in-depth answer than I'm going to provide, but I'll go ahead and make a few points anyway:
void someFunc(int array[])
{
// Where does the number '256' come from? Even if you make it a named
// constant (which is advisable), then a) you're still stuck with a
// fixed-size array (which may or may not be a problem for you), and
// b) you can only use this function with arrays of that size.

// You could pass the size in as an argument, but that's cumbersome and
// error-prone.

// Fortunately we have std::vector, which knows its own size. And, if
// you really do want a fixed-size array, you can use boost::array,
// which, again, knows its own size :)
for (int i=0; i<256; i++) {
DoStuffOnInt(array);
}
}

// Compare to:
void someFunc(std::vector<int>& array)
{
for (int i = 0; i < array.size(); ++i) {
DoStuffOnInt(array);
}
}
There are other points that could be addressed here as well (dynamic memory management for variable-sized arrays, bounds checking [see vector::at()], genericity, and so on), but it would take a fairly lengthy post to address them all.

Share this post


Link to post
Share on other sites
Quote:
Original post by EasilyConfused
Because, in this instance, you have to trust that the calling code has passed an array with at least 256 elements to avoid an access violation, and if you change the size of the array this function operates on, you have to ensure everything that calls it is updated as well.


This is a correct reason for not using his code, but not a good reason for using std::vector: you can always ensure the size of an array by using an array reference (int (&array)[256] is enforced by the compiler as containing exactly 256 elements). In this particular case, there's no reason for using std::vector—both the array and the vector are good solutions to the issue of updating an array of 256 elements.

Of course, if you wish to write a general function that will work for any number of objects, then the correct C++ solution is:

template<typename It>
inline void someFunc(It begin, It end)
{
std::for_each(begin, end, &DoStuffOnInt);
}

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!