Determining Stack Space with Visual Studio

前端 未结 4 707
耶瑟儿~
耶瑟儿~ 2020-11-30 07:05

I\'m programming in C in Visual Studio 2005. I have a multi-threaded program, but that\'s not especially important here.

How can I determine (approximately) how muc

4条回答
  •  醉梦人生
    2020-11-30 07:48

    You can make use of information in the Win32 Thread Information Block

    When you want in a thread to find out how much stack space it uses you can do something like this:

    #include 
    #include 
    #include 
    
    inline NT_TIB* getTib()
    {
        return (NT_TIB*)__readfsdword( 0x18 );
    }
    inline size_t get_allocated_stack_size()
    {
        return (size_t)getTib()->StackBase - (size_t)getTib()->StackLimit;
    }
    
    void somewhere_in_your_thread()
    {
        // ...
        size_t sp_value = 0;
        _asm { mov [sp_value], esp }
        size_t used_stack_size = (size_t)getTib()->StackBase - sp_value;
    
        printf("Number of bytes on stack used by this thread: %u\n", 
               used_stack_size);
        printf("Number of allocated bytes on stack for this thread : %u\n",
               get_allocated_stack_size());    
        // ...
    }
    

提交回复
热议问题