Now that I have your attention
I am a first year uni student and had to make a c program for an assignment. Have run into problems though with pointers changing (mebe just breaking?) at a strange point where I should (read _should_ lol) have locked down all the random memory writes. I would normally just trawl forums / google looking for answer to something like this but feel here it is too ambiguous and strange to get something out of it. So here goes.
A bit of background: The program is a small 2d RPG that has a 40x40 map array and a 40x40 monsters / items array. It has a function GenerateMap that poplulates both these arrays at the start of the game. I then has the ability to go up and down levels via a trigger
that recalls GenerateMap with a different seed index.
It did originally work, but I changed the map access with the original being (simplified e.g.)
if (map[x][y] == 'g') //read
map[x][y] = 'x' //write
Like I said this worked fine but some parts of the map were obviously reading / writing outside the map. It worked, but badly. I changed it so that this became part of wrapper modules ReadMap() and WriteMap() that preformed error checking in the basic form
char ReadMap(map, x, y)
if (within bounds)
return map[x][y]
and the same for writing to the map. Now I don't have any problems with writing to strange areas of RAM and know that the rest of the program isn't going to be modified when writing to the map.
The problem is this:
void RunTests() is used to check among other things whether a map change is required. If it is it increases the map seed number and reruns GenerateMap(). This works out fine, and returns back to RunTests without error. Exiting out of RunTests however the pointer to monstersItems() is destroyed. Instead of eg 0x7fffa0df6520 it is changes to 0x7fffffffffff. Somehow the program stack is being destroyed from outside what is writeable. Now when ReadMap() or WriteMap() are run the XY values are ok but the pointer is outside of what the program can read and it segfaults. I have run kdbg all over
this program but have no idea what the problem is here.
So to recap
GameState()
//pointer to monstersitems is fine here
<do stuff>
RunTests()
//pointer points to wrong address
//next ReadMap() or WriteMap() segfaults as we are reading outside of program block.
RunTests()
if (need level change)
//pointer to monsters is fine here
GenerateMap(mapSeed + 1)
//pointer is still fine
I've included my full source for you to look at but appreciate the time it takes to identify with the code and find errors. Like I said I normally don't just go on forums and say "this is broke, how to fix lol" but in these extenuating circumstances I can't see my normal forum trawl helping much. If anyone could give some description of the problem itself so that I could research it would be greatly appreciated.
[edit] Also again some things may be done a bit strangely / badly. This is my first major c project, I'm still learning. feel free to tell me what is wrong and if you can be bother how / why it can be made better.
[edit] Gah sent production version with my full name / student number int it. Any way to delete? Ok got it.
[another edit] One dodgy way I though around this would be in GameState() to back up the pointer, run it, and copy back.
tmpPoint = &monstersItems;
RunTests;
&monstersItems = tmpPoint;
Is this possible? Would this work? I have no idea how I would code-wise go about this, but that's nothing half an hour on google wouldn't fix


LinkBack URL
About LinkBacks





Reply With Quote







Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum