如何检测我的程序可用的堆栈空间量? [英] How to detect the amount of stack space available to my program?

查看:900
本文介绍了如何检测我的程序可用的堆栈空间量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Win32 C ++应用程序充当RPC服务器 - 它有一组用于处理请求的函数,RPC运行时创建一个单独的线程并调用该线程中的一个函数。

My Win32 C++ application acts as an RPC server - it has a set of functions for processing requests and RPC runtime creates a separate thread and invokes one of my functions in that thread.

在我的函数中,我有一个std :: auto_ptr,用于控制在编译时已知的堆分配的char []数组大小。它在使用VC ++编译时意外工作,但它是根据C ++标准的未定义的行为,我想以清除它。

In my function I have an std::auto_ptr which is used to control a heap-allocated char[] array of size known at compile time. It accidentially works when compiled with VC++ but it's undefined behaviour according to C++ standard and I'd like to get rid of it.

我有两个选项:std :: vector或堆栈分配的数组。因为我不知道为什么有一个堆分配的数组,我想考虑用堆栈分配的替换它。该数组是10k元素,如果RPC运行时生成一个非常小的堆栈线程,我可以假设面临堆栈溢出。

I have two options: std::vector or a stack-allocated array. Since I have no idea why there's a heap-allocated array I would like to consider replacing it with a stack-allocated one. The array is 10k elements and I can hypothetically face a stack overflow if the RPC runtime spawns a thread with a very small stack.

我想检测多少堆栈空间被典型地分配给线程,并且它的多少可用于我的函数(它的被调用者肯定消耗一些分配的空间)。

I would like to detect how much stack space is typilcally allocated to the thread and how much of it is available to my function (its callees certainly consume some of allocated space). How could I do that?

推荐答案

我不知道你在做什么。

I'm not sure what you're after.

如果你只是想获得典型的数字,那么请继续尝试!创建一个具有嵌套作用域的函数,每个函数都分配一些堆栈空间。每个范围中的输出。看看事情得到了多远。

If you just want typical numbers, then go ahead and try! Create a function with nested scopes, each of which allocates some more stack space. Output in each scope. See how far the thing gets.

如果你想在具体的情况下具体的数字,问自己一旦你有他们想做什么?分支到不同的实现?这听起来像一个维护问题,其使用应该是非常好的理由。你期望获得什么?这真的值得这么麻烦吗?

If you want concrete numbers in a concrete situation, ask yourself what you would want to do once you have them? Branch into different implementations? This sounds like a maintenance problem the use of which should be very well justified. What do you expect to gain? Is this really worth such a hassle?

我同意10k通常不应该是一个问题。所以如果你的代码不是任务关键,使用 boost :: array (或 std :: tr1 :: array ,如果你的std lib自带了它)。否则只需使用 std :: vector 或者,如果你觉得必须, boost :: scoped_array $ c> std :: tr1 :: scoped_array ,如果你的std lib附带它)。

I agree that 10k usually shouldn't be a problem. So if your code isn't mission critical, go ahead and use boost::array (or std::tr1::array, if your std lib comes with it). Otherwise just use std::vector or, if you feel you must, boost::scoped_array (or std::tr1::scoped_array, if your std lib comes with it).

这篇关于如何检测我的程序可用的堆栈空间量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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