I've got a function that is supposed to recursively find a path from beginning (0,0) to end (9,9). He does it, just very slow right now. Within my findPath function, I'm passing it a 2d boolean array, which is just there so it knows which tiles its been on, and not to go on them again.
Right now, its making changes to the original table I instantiated, rather than the one I passed in my functions, so instead of getting a different table for each recursive movement, I've got one table for all of them, and hes lucky that hes funding his way across right now.
According to my debugger, once my table gets passed to the function, it goes from being bool[10][10] to bool[10]*, causing everything to crap out.
Anyone got any ideas, or do ya wanna see the code?
Posts
I have to admit that I can't remember off the top of my head what C++ will and will not pass by copy. You may have to manually copy the array before passing it to the function. Just be sure to de-allocate the memory once the function is done with it!
edit: quick thought - since this array is fixed size, you could just declare an array at the beginning of the function and do the copying there. That should be easier, especially if you aren't comfortable with memory management.
Out of curiosity, what's your algorithm? It sounds like your program is doing a depth-first search, which means it's not guaranteed to return the shortest path. Is that a concern?
Something like this:
Now you can pass bArray to your function and be able to access it as bArray[0][0].
DISCLAIMER: This code has not been compiled, but gcc should take it. VC++ 6.0 probably won't though as it doesn't do variable scope correctly. To fix that just declare i at the same time as bArray and it should work. I don't know about VC++ .net; never used it.
Also, this code creates 10 individual arrays that do not necessarily follow each other in memory. Declaring bool someArray[10][10] on the other hand, guarantees that all elements follow each other in memory, and allows a single memcpy() from the original if needed.
To be honest, I do it that way because I think it's easier. When I'm passing a pointer, I never have to remember if I passed by value or reference or any of that. I know that when I pass a pointer, I can access the same data in the called function that I accessed in the calling function.
Besides, it sounds like OP is in a C++ programming class, and good memory management is going to be handy to know when these arrays are no longer [10][10] but [x][y].
Right now my algorithm finds ALL paths, and I have a functions to tell me what the best path is.
See how many books I've read so far in 2010