### Too much recursion with vector(stack overflow)

Hello, I have a vector to draw some squares to the screen and my program compiles fine but when I run it, it pops up a window that says "Unhandled exception at 0x5a2b5017 in BeerPong2.exe: 0xC00000FD: Stack overflow." I know the memory locations are useless to help, but I wanted to put the whole message up there and my compiler gives me "The program '[5744] BeerPong2.exe: Native' has exited with code -1073741510 (0xc000013a)." So I've heard that a stack overflow means there is too much recursion, so I had all of this in the initialization function because I figured that it was because I was using the same function in itself. So I moved it out of the initialization and made it its own function and called it in the initialization, but I got exactly the same messages. So does anyone know a way to reduce recursion? Or should I make a new class and put this in it? Or any other suggestions would help.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374`` ``````void Bottle::DrawBottle() { std::vectorbottle(244); float by = 35; float bx = 165; int bottleIndex = 0; for(int i = 0; i <= 5; i++) { bottle[bottleIndex].SetPosition(bx, by); bottleIndex++; bx += 15; } by += 15; bx = 180; for(int i = 0; i <= 10; i++) { for(int j = 0; j <= 3; j++) { bottle[bottleIndex].SetPosition(bx, by); bottleIndex++; bx += 15; } by+=15; bx = 180; } bx = 165; for(int i = 0; i <= 5; i++) { bottle[bottleIndex].SetPosition(bx, by); bottleIndex++; bx += 15; } by += 15; bx = 150; for(int i = 0; i <= 7; i++) { bottle[bottleIndex].SetPosition(bx, by); bottleIndex++; bx += 15; } by += 15; by = 135; for(int i = 0; i <= 16; i++) { for(int j = 0; j <= 9; j++) { bottle[bottleIndex].SetPosition(bx, by); bottleIndex++; bx += 15; } by += 15; bx = 135; } bx = 150; for(int i = 0; i <=7; i++) { bottle[bottleIndex].SetPosition(bx, by); bottleIndex++; bx += 15; } }``````
Last edited on
> So does anyone know a way to reduce recursion?
¿why do you have recursion?
Also, ¿where? I don't see a call to `DrawBottle'

 ``1234`` ``````void Bottle::DrawBottle() { std::vectorbottle(244);``````
So you've got a bottle of bear and you want to drink it.
So you go to the market and buy another 244 bottles first
that doesn't seem sane.
Last edited on
Well to be honest, I wasn't sure I was using recursion, but when I searched the error messages online, most of the posts said when you get a stack overflow, it means you have too much recursion. So if this is not what you're asking, then I have recursion because I want the bottle to be drawn to the screen as it is called. As to where, I was kind of curious myself, I originally used all of this function in the initialization function, and then I got the error messages, so I moved everything to it's own function to try to fix it, but I got the same error messages. and I don't understand your analogy, is that not how you are supposed to call a vector?
 Well to be honest, I wasn't sure I was using recursion

You are not. At least not in this function.

 So if this is not what you're asking, then I have recursion because I want the bottle to be drawn to the screen as it is called.

Feel free to look up the meaning of recursion.

 I don't understand your analogy, is that not how you are supposed to call a vector?

In Bottle::DrawBottle, you create a local vector of 244 bottles and draw them. Typically a "draw" function for an object should only "draw" the "bottle" it was invoked on. Then again, it's hard to see why this would be a member of a class since it doesn't access any class members at all. Is Bottle a namespace?
Last edited on
@OP: run your program trough a debugger and perform a backtrace when it crashes.
If you have `too much recursion' you'll see quite a long call stack.
Regardless, it would tell you where the crash occurs, so post that function.
cire: Well Bottle is a class, and I want the vector to be objects of the class type.

ne555: the call stack was extremely long, but I still couldn't tell where it crashed though.
¿what are the functions on top of the stack?
Topic archived. No new replies allowed.