如何使用vc ++找出当前线程的堆栈剩余多少空间? [英] How can I find out how much space is left on stack of the current thread using vc++?

查看:125
本文介绍了如何使用vc ++找出当前线程的堆栈剩余多少空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VC ++ 2012.
我想知道当前线程中有多少堆栈内存。

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

快速搜索点使用malloc.h和stackavail()函数,但它不在Visual C ++ 2012.如何以另一种方式实现这个?

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?

问题的例子是: / p>

Example in question is this:

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

using namespace std;

int _tmain()
{
    cout << "Available stack: " << stackavail() << std::endl;
}


推荐答案

线程安全,不需要asm内联。我不认为我们那些需要跟踪堆栈需要精度。只是一个很好的估计,可用的,以防止溢出发生。我们需要跟踪它,因为我们为用户提供了创建可能使用递归或其他服务或需求的宏,脚本,表达式等的能力。每个环境都应该能够报告堆栈可用性,即使它只是使用所有可用的内存,所以任何递归都可以控制。

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;    
}

这篇关于如何使用vc ++找出当前线程的堆栈剩余多少空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆