I use Windows 10, Visual Studio 2019 The program generates threads. I need to add functionality pointing me what is available stack size in any execution time .
#include <iostream>
#include <thread>
void thread_function()
{
//AvailableStackSize()?
//Code1 //varibales on stack allocation + function call
//AvailableStackSize()? it should decrease
//Code2 //varibales on stack allocation + function call
//AvailableStackSize()? it should decrease
//Code3 //varibales on stack allocation + function call
//AvailableStackSize()? it should decrease
}
int main()
{
std::thread t(&thread;_function);
std::cout << "main thread\n";
std::thread t2 = t;
t2.join();
return 0;
}
I try to use . But I am not sure how can I proceed. I just can query what is a total size of stack. Not an available one.
bool AvailableStackSize()
{
// Get the stack pointer
PBYTE pEsp;
_asm {
mov pEsp, esp
};
// Query the accessible stack region
MEMORY_BASIC_INFORMATION mbi;
VERIFY(VirtualQuery(pEsp, &mbi, sizeof(mbi)));
return mbi.RegionSize;//This is a total size! what is an available? Where guard is located?
}
Probably I can Also check if some address in inside a stack
PVOID add;
(add>= mbi.BaseAddress) && (add < PBYTE(mbi.BaseAddress) + mbi.RegionSize);
I saw several similar question, but no one answers the question 100%. What is correct approach to get available stack size?