是否可以确定堆栈上有多少空间? [英] Is it possible to determine how much space is available on the stack?

查看:136
本文介绍了是否可以确定堆栈上有多少空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个小型软件引擎,我想使用堆栈来快速迭代大数字集合。但是后来我发现这可能是一个坏主意,因为堆栈不像堆那样大的内存存储。但是我被堆栈的速度和缺乏动态分配编码实践吸引。

I'm architecting a small software engine and I'd like to make expensive use of the stack for rapid iterations of large number sets. But then it occurred to me that this might be a bad idea since the stack isn't as large a memory store as the heap. But I am attracted to the stack's speed and lack of dynamic allocation coding practices.

有没有办法找出我可以在一个给定的平台上推栈多远?我主要看移动设备,但问题可能出现在任何平台上。

Is there a way to find out how far I can push the stack on a given platform? I am looking mainly at mobile devices but the issue could come up on any platform.

推荐答案

在* nix上,使用 getrlimit

On *nix, use getrlimit:

   RLIMIT_STACK
          The maximum size of the process stack, in bytes.  Upon
          reaching this limit, a SIGSEGV signal is generated.  To handle
          this signal, a process must employ an alternate signal stack
          (sigaltstack(2)).

在Windows上,使用 VirtualQuery

On Windows, use VirtualQuery:


它将堆栈上的任何值的地址赋给
,以获得提交的堆栈空间的基址和大小(以字节为单位)。
在堆栈向下增长的x86机器上,从基地址和VirtualQuery中减去大小
:这将为堆栈留出的空间大小为
(假设你' re not
正好是当时堆栈大小的限制)。

For the first call, pass it the address of any value on the stack to get the base address and size, in bytes, of the committed stack space. On an x86 machine where the stack grows downwards, subtract the size from the base address and VirtualQuery again: this will give you the size of the space reserved for the stack (assuming you're not precisely on the limit of stack size at the time). Summing the two naturally gives you the total stack size.

没有与平台无关的方法,因为堆栈大小是左边的到逻辑上的实现和主机系统 - 在嵌入式迷你SOC上比128GB RAM服务器上分配的资源更少。不过,您可以通过API特定的调用来影响所有操作系统上特定线程的堆栈大小。

There is no platform-independent method since stack size is left to the implementation and host system logically - on an embedded mini-SOC there are less resources to distribute than on a 128GB RAM server. You can however influence the stack size of a specific thread on all OS'es as well with API-specific calls.

这篇关于是否可以确定堆栈上有多少空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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