Increase stack size when compiling with mingw?

2019-01-15 09:55发布

问题:

I'm writing a recursive flood-fill algorithm to find connected components in an image, my code compiles and runs well with MSVC 2008 compiler; but the mingw-compiled binary crashed at runtime.

After I converted the algorithm to non-recursive with std::stack, everything goes well.

But what if I must use recursive algorithm in some case, and mingw cannot handle it?

How can I increased stack size of a binary, is there any compilation options?

Thanks

回答1:

Use

gcc -Wl,--stack,N

where N is stack size. E.g. gcc -Wl,--stack,4194304



回答2:

probably the best bet is to use pthreads to start a new thread and run your algorithm in the new thread. One of the parameters to pthread_create is pthread_attr_t. Using this attribute you can specify the stack size (by calling pthread_attr_setstacksize).

Edit: Whether this works or not is dependent on support of the underlying compatibility layer



回答3:

Maybe increasing stack size is not the solution you want. These restrictions do exist for a reason. It also may happen that in a near future your algorithm will use even more stack space and you will have to increase it again.

Perhaps you should consider converting your algorithm into a non-recursive one. This can be done for every algorithm. See this discussion

And you will probably gain a performance improvement also