Colobot Forum - International Colobot Community
Stack overflow in Colobot - Printable Version

+- Colobot Forum - International Colobot Community (https://colobot.info/forum)
+-- Forum: [Archive] New forum (2015-2019) (https://colobot.info/forum/forumdisplay.php?fid=76)
+--- Forum: Colobot: Gold Edition Basics (https://colobot.info/forum/forumdisplay.php?fid=58)
+---- Forum: General (https://colobot.info/forum/forumdisplay.php?fid=59)
+---- Thread: Stack overflow in Colobot (/showthread.php?tid=867)



Stack overflow in Colobot - Smok - 12-09-2016

Hi. I was trying my new way to write programs i colobot, but it made me pretty fast face stackoverflow. I decided to write program like this:

Code:
public void object::smartGrab(point p)
{
    goto(p);
    grab();
}
Then do stack of overloading functions.
Code:
public void object::smartGrab(object o)
{
    smartGrab(o.position);
}
public void object::smartGrab(int cat)
{
    smartGrab(radar(cat));
}
And then i use my smartGrab(TitaniumOre); in my collectTitanium function, and then i use my collectTitanium in smartBuild function if there is no titanium, and then i use my smartBuild function in makeBot function if there is no BotFactory, and so on.
I found this way of programming super comfy, but it made me hit stackoverflow realy fast (like on 10th nested function). Is my way of programming bad or CBot have very small stack size? How i should deal with this problem?  Confused


RE: Stack overflow in Colobot - krzys_h - 12-09-2016

I'm pretty sure the limit is about 1000 stack frames (but do keep in note that there are multiple internal stack frames per function call). Are you sure that your functions do indeed get called as expected and you are not running into some other bug that causes the recursion to loop infinitely? If you still think there is something wrong with the stack limit, try to reproduce this issue on a smaller program so we can test it easier.

Just looked into the code to confirm, I was correct with the stack limit - https://github.com/colobot/colobot/blob/master/src/CBot/CBotDefines.h#L23


RE: Stack overflow in Colobot - Smok - 12-10-2016

I rewited my program and now is ok. I probably messed up and made recursion somewere. btw. Its good to know where it is set in game code. Thanks.