I am trying to make a maze solving program using c++ and a stack. It will get to the end of the maze, but then it cannot trace back its steps. First, here is some code -
When I am going through, the print statement in the Position constructor says that the correct values for the previous positions are being set. However, the print statement before the second while loop in the traverse function says that the previous position is not correct. Then I am not able to trace back. I have not been able to figure out why and where the previous member for the Positions change. I think it has something to do with having a Position data member for each Position instance. Maybe the pointers are being deleted and changed that way or something? Maybe the local Position temps are being deleted after I push them onto the stack? I don't know, I have tried multiple different approaches that I could think of and am pretty stuck. If anyone can help me out I would greatly appreciate it, thanks.
I have not been able to figure out why and where the previous member for the Positions change.
That is what a debugger is for.
Have you used the debugger that comes with your compiler? If not, please learn to use it as it is a mandatory tool that every programmer must use to debug programs.
I think it has something to do with having a Position data member for each Position instance. Maybe the pointers are being deleted and changed that way or something?
To remove all doubt, use the debugger.
Last, C++ already has a std::stack class. Why did you need to create your own when one already exists?
I was not aware a std::stack existed. I made my template to 1) better learn how it works and 2) always have one that I knew exactly what was going on. I have used debuggers in the IDE GUIs (setting breakpoints and looking at data in the ide's window) I used before in the past, but not gdb in the terminal. I have been having trouble working my way around gdb so far.
I found another way around the problem though. I did not find where the segmentation fault was occurring, but I realized having a Position* member in the class was just really unnecessary. I can just pop off the stack and get the path...
I was not aware a std::stack existed. I made my template to 1) better learn how it works and 2) always have one that I knew exactly what was going on. I have used debuggers in the IDE GUIs (setting breakpoints and looking at data in the ide's window) I used before in the past, but not gdb in the terminal. I have been having trouble working my way around gdb so far.
Well, without the debugger, it is very difficult to solve programming problems.
I found another way around the problem though. I did not find where the segmentation fault was occurring, but I realized having a Position* member in the class was just really unnecessary. I can just pop off the stack and get the path...
What is "positionValid", and why are you creating a Position dynamically like this in cascading if() statement? This is more than likely a memory leak. Unless the Position() class somehow stores its instances in some global container of Position pointers, that is a memory leak.
So the reason why your fix works is that you removed the usage of pointers and instead used value-based semantics. This is how you reduce or eliminate pointer bugs that I endorse. However, you really do need to brush up more on how to handle dynamically allocated memory properly, since at some point you will have to call "new" and use pointers for legitimate and necessary reasons.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; June 15th, 2011 at 03:06 PM.
Bookmarks