C++ code unable to run, exception thrown

My code won't compile. It reaches a stack overflow in some variable right before it hits the first function, readmap. Although according to the watch list, my map array, is still shown as properly initialized with all zeroes and the other variables still contain proper values. Disregard printmap, and descent, the code isn't finished.

The program is supposed to accept .ppm files, convert the data to a 2D array, any pixels that are white are empty, any other color considered a wall and set as -1. Then, with user input obtain a starting location, and an ending location from which it is to find the shortest path from start to finish.



Sorry but this is unreadable. If you want help you should post the code in the right format or maybe as a link to the src-file.

pastebin is your friend

seriously put it on pastebin! this should be a rule for all code posting is put it on pastebin



sorry, i didn't notice the format so late at night

oh god my eyes

So does it not compile or does it stack overflow? Pick one ;)

It wouldn't compile on my machine, but that was because it was missing the cstring header include. I guess MSVC++ is quirky and includes it automatically. So I compiled it, and it actually even kinda almost ran once. Then crash and burn. Then I looked at the code...

C++ has a handy string library so you don't have to mess with C strings...


The delimiting character is the newline character ('\n') for the first form, and delim for the second: when found in the input sequence, it is extracted from the input sequence, but discarded and not written to s.

And it would make more sense to use a do { ... } while ( ... ); loop when reading the input filename. And why is map[][] static?? It's in main(), that makes no sense! D:

Well I haven't had to do any programming in a while, so I'm taking the liberty of rewriting things to use standard strings and not be so terribly nonsense (no offense).


I didn't dive into the actual meat of the code (the image processing logic) but it gets to where it (incorrectly) reads the file at least.

What's this code for? School? I hope you will take a look at the changes I've made and study the classes and methods I used so that you understand how it works and why I did it that way. Missing the do { ... } while( ... ); shows that you are probably pretty new at this programming thing. Don't be discouraged, just keep absorbing as much knowledge as you can get your hands on. The cplusplus.com site I linked to above is really helpful. I sometimes like to just explore different classes to get an idea of the methods available and the general conventions used by the C++ standard library.

It builds, but when running the debugger, the program could only go up to the line of the cout "potato" as soon as it hit the readmap function, an "unhandled exception blahablahblah: Stack overflow.


that the screenshot of the error


Right well try stepping through with the debugger to see exactly what the problem is, then fix it. I'm not going to teach you how to use Microsoft's stuff though, you're on your own there.

I notice when I run it that it tells the wrong dimensions for the PPM. But I guess if you are seeing potatos you aren't talking about my version of the code. TBH IDK why microsoft lets you compile "char temp2[1000] = { ".ppm" };" but think about it for a few minutes and see if maybe it doesn't strike you as a bit off. Also try looking at what I posted because it avoids this problem entirely.

Thank you for the example, you are right in assuming that i am new. And this is for a class, of the three programs that were assigned over the semester this is the first one that i have struggled with. I will look into the new classes and methods that you used in your code. 

Freq Labs, pastebin? codepad.org.

Note the warmings, I'd paste them but the formatting is just lost. concatenate the lines! *sigh*

And I fail to see any classes in your revision of the code. Class, function, method? What is the difference, yet you niggle at the compile/overflow issue. Same differences!


DarthusianLord, use more appropriate names for variables. These are meaningless :-

  • temp
  • temp1
  • tmp

ln 60, int pixel. Why pixel, it represents many pixels name it as such. And that isn't how you 'initiate' an array, might as well just be

  • int pixel[600][800][3];
struct rgb {
  int red;
  int green;
  int blue;
rgb pixels[600][800];
That is if you really want red, green and blue in separate variables. You could bit-shift one int for all 3 colours.

int readmap(), return boolean rather than int. And why is only one dimension of map declare, either declare both or non at all.

int fill(), lovely nested for/if's. Perhaps you could break it down into more functions? It will make the code easier to read.

  • if (map[i][j] == count) - Excuse me, what? Map should either be 0 or -1 yet count goes UP! What exactly are you trying to achieve within this code? Comments would be helpful and as I said before perhaps extract some aspect into another function.
  • while (check == false) and check = true; - You know you can use break; within a while loop, right?

Cool, haven't seen codepad before!

I don't know what you're talking about with classes/functions/methods being all the same or not seeing where I used any classes. I used the string class, which made a huge impact on the cleanliness and clarity of the code.

Failing to compile and producing a stack overflow are mutually exclusive results. I didn't mean any harm by pointing that out.

btw, microsoft compiles char temp2[1000] = { ".ppm" }; because only the first 4 spots of the array get taken, everything else is blank space. 

Huh that's new to me. It looks like it would initialize an array of c-strings, but apparently it's exactly the same as just doing char str[100] = "msg"; I never knew that! :o Holy crap it even does that in C! *shocked* I've always just done it without the braces because it doesn't make sense to me the other way. Same with the next variable, pixel. My mind is blown right now. What the hell have I been using calloc and loops for all these years?? D:

OK I tried running the original code you posted (interestingly it compiles unchanged on FreeBSD, maybe because it uses Clang instead of GCC?). I don't get a stack overflow, it plows through the code like it aint no thang.


I'll fire up a Windows VM to see if I can reproduce your error using Visual Studio. This is starting to get interesting...