5

I am using VC++ 2012. I would like to be able to tell how much stack memory is available in current thread.

Quick search points to using malloc.h and stackavail() function, yet its not there in Visual C++ 2012. How do I achieve this in another way?

Example in question is this:

#include "stdafx.h"
#include <iostream>
#include <malloc.h>

using namespace std;

int _tmain()
{
    cout << "Available stack: " << stackavail() << std::endl;
}
enko
  • 615
  • 6
  • 20

3 Answers3

5

This uses some stack but is thread-safe and doesn't require asm inline. I don't think those of us that need to track the stack need precision. Just a good estimate of what's available to prevent an overflow from occurring. We need to track it because we offer users the ability to create macros, scripts, expressions, etc.. that may use recursion or other services or needs. Every environment should be able to report stack availability even if it just uses all available memory so any recursion can be controlled.

size_t stackavail()
{
  // page range
  MEMORY_BASIC_INFORMATION mbi;                           
  // get range
  VirtualQuery((PVOID)&mbi, &mbi, sizeof(mbi));           
  // subtract from top (stack grows downward on win)
  return (UINT_PTR) &mbi-(UINT_PTR)mbi.AllocationBase;    
}
user3161924
  • 1,849
  • 18
  • 33
  • Thank you, I'll test it when my attention makes it back to c++, and mark this as answer. – enko Feb 18 '14 at 21:43
2

There is no such function as stackavail() in C++, though some compilers, such as "Open Watcom C++" provide it as an extension.

If you really need to know this information, use an OS-specific system call to figure it out.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • @Seth: I'm trying to make a deliberate point that "C++" *is* what is defined in the standard. Thanks, though. – Lightness Races in Orbit Oct 15 '12 at 23:19
  • 1
    Ok, I was just trying to be specific (i.e. I can write a function called `stackavail` and then there will such a function in C++), but if you think it's good then alright. – Seth Carnegie Oct 15 '12 at 23:21
  • @Seth: I see where you're coming from. Personally I would never call it even then "a function in C++", but I can understand how someone might mean it that way if they might otherwise have said "a function written in C++" or "a function in a C++ program". – Lightness Races in Orbit Oct 16 '12 at 00:42
1

Ok so these are my findings so far.

There is no easy one function way of checking stack space via vc++ on windows.

But I found an answer elsewhere.

size_t stackavail()
{
    static unsigned StackPtr;   // top of stack ptr
    __asm mov [StackPtr],esp    // mov pointer to top of stack
    static MEMORY_BASIC_INFORMATION mbi;        // page range
    VirtualQuery((PVOID)StackPtr,&mbi,sizeof(mbi)); // get range
    return StackPtr-(unsigned)mbi.AllocationBase;   // subtract from top (stack grows downward on win)
}

Additionally:

In windows/vc++ by default stack space is set at 1MB per thread. To set it higher for main() thread you have to compile via linker flag of /STACK:#### which is rounded to nearest 4. Ex: /STACK:2097152 for 2MB stack.

Hope this helps someone.

enko
  • 615
  • 6
  • 20
  • 1
    You were asking about stack available for 'current thread', which implies that you want to use it in a multi-threaded application. If this is the case I would strongly suggest to fix your 'stackavail' function, because it is definitely NOT THREAD SAFE. – sirgeorge Oct 17 '12 at 21:42