Windows环境下设定GCC的栈空间大小

过去一年在研究二维条带装箱问题的精确搜索算法。写了一个迭代加深搜索,发现自己的程序运行大规模数据的时候总会出现一些莫名其妙的错误,分析之后发现是栈空间不够。按道理说该问题的深度优先搜索算法的空间复杂度应该是O(n·f(n)),其中n是问题规模,表示当前找到一条从根节点到解的长度为n的路径,f(n)是一个关于n的多项式函数,表示每个节点使用的数组的大小,应该不容易溢出才对。

之前网上的文章多数是传授如何在VC下更改栈空间,而我使用的编译器是GCC,相关文章很少。搜索一番以后得出如下结果

以下摘自 https://gcc.gnu.org/onlinedocs/gcc-4.0.1/gnat_ugn_unw/Setting-Stack-Size-from-gnatlink.html

“Under Windows systems, it is possible to specify the program stack size from gnatlink using either:

  • using -Xlinker linker option
              $ gnatlink hello -Xlinker --stack=0x10000,0x1000
    

    This sets the stack reserve size to 0x10000 bytes and the stack commit size to 0x1000 bytes.

  • using -Wl linker option
              $ gnatlink hello -Wl,--stack=0x1000000
    

    This sets the stack reserve size to 0x1000000 bytes. Note that with -Wl option it is not possible to set the stack commit size because the coma is a separator for this option.”

  • 总结一下,就是在调用连接器(linker)的时候加上运行参数
    -Wl,--stack=SIZE_STACK

    其中SIZE_STACK是待设定的栈空间大小,如果用DEV-C++的话可以在“编译器设置”栏中设定。需要注意的一点是,设置比较大的栈空间的时候需要使用64位位宽(mx)编译程序,在DEV-C++的编译器设置栏目中仍然可以找到。

Leave a Reply

Your email address will not be published. Required fields are marked *

Protected with IP Blacklist CloudIP Blacklist Cloud

This site uses Akismet to reduce spam. Learn how your comment data is processed.