问题:怎样才能得到一个函数使用的Stack空间大小呢。解释:Stack空间大小指的是程序运行到这个函数时,函数运行需要使用的运行时栈的大小。
例如:
function()
{
   int i, j;  // 运行时栈中的空间,这就是我需要计算的空间大小。
   
   int k = new int[1]; // 堆中的空间,这不是我需要计算的空间大小。   ...
}我知道能够在Unix上面取得这个大小,那么,我想在Windows上面应该同样能够取得这个数值,那么VC编译器是否提供这样的功能呢?或者Windows或VC的库中是否有取得一个函数使用的Stack空间大小的函数呢?目的:统计一个数万行程序中,所有函数的运行期Stack的大小。以上问题,希望大家能够不吝指导,谢谢!

解决方案 »

  1.   

    栈的空间大小有限定,vc的缺省是2M。栈是由编译器自动管理的
      

  2.   

    unsigned stacktop;
    unsigned stackbottom;void checkstack()
    {
        int i;    unsigned s = (unsigned) & i;    if (s < stackbottom) stackbottom = s;
    } void f()
    {
        int i, j;    checkstack();
    }int main()
    {
        int i;    statcktop = stackbottom = (unsigned) & i;    f();    printf("Maximum stack depth %d", stackbottom - stacktop);    return 0;
    }
      

  3.   

    谢谢FengYuanMSFT的聪明的方法:)不过我不可能每个函数都这样写上一些代码来计算Stack大小的,既然VC中没有提供这样的功能,Windows中也没有这样的函数,我就像别的方法,比如在Unix上面计算吧。谢谢大家的帮助,麻烦你们了!
      

  4.   

    Check for /Gh compiler option in VC. A call to _penter will be added to each function. Then you can do stack measurement there.Read a related article: http://www.fengyuan.com/article/profile.htmlIf your program has recursive functions, it's very hard to do static analysis.If you just want stack usage for each function, you may be able to get information from .map file or debug symbols.