在C#中分配堆栈大小?
我正在为我一直在研究的字节代码语言开发虚拟机。我正在使用 System.Collections.Generic.Stack 类作为堆栈,但是有什么方法可以分配堆栈大小吗?或者我只需要编写自己的堆栈对象即可使用?
I am developing a virtual machine for a byte code language I have been working on. I am using the System.Collections.Generic.Stack
class for the stack but is there any way to allocate the stack size? Or do I just have to write my own stack object to use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
来自 Stack(Int32) 构造函数的 MSDN:
From MSDN for the Stack(Int32) constructor:
您可以使用初始容量构建它,但堆栈将随着需要(当您向其中添加项目时)。
如果您不希望它在超过其容量时自动添加新项目,则需要将其封装在您自己的类中或编写自己的堆栈来删除多余的项目。
You can construct it with an initial capacity, but the stack will grow as needed (as you add items to it).
If you do not want it to automatically add new items when you push it past it's capacity, you'll need to encapsulate it in your own class or write your own Stack which removes the excess items.
您可以使用 Stack构造函数 (Int32) 用于指定堆栈的容量:
请注意,如果添加超过 1024 个项目,堆栈的容量将会增加。如果你不想这样,你可以在每次推送之前检查堆栈大小:
You can use the Stack<T> Constructor (Int32) to specifiy the capacity of the stack:
Note that the stack will grow the capacity if you add more than 1024 items. If you don't want this, you can check the stack size before each push:
您可以 - 请参阅 http://msdn.microsoft.com/en-us/library /ahc986x9.aspx
you can - see http://msdn.microsoft.com/en-us/library/ahc986x9.aspx
您的术语还有很多不足之处,但是
Stack
有一个构造函数,允许您传递初始容量:http://msdn.microsoft.com/en-us/library/ahc986x9.aspx堆栈可以随内存大小自由增长,因此您不用担心这个!
Your terminology leaves a lot to be desired, but
Stack
has a constructor that allows you to pass an initial capacity: http://msdn.microsoft.com/en-us/library/ahc986x9.aspxThe stack is free to grow as large as you have memory though, so you don't need to worry about that!
堆栈(T)类
它会动态地自行调整大小。但如果您想预分配其大小,请查看有关堆栈构造函数的文档。您可以使用重载来设置堆栈的初始大小。
Stack(T) Class
It resizes it self dynamically. But if you want to preallocate its size then check out the documentation on the Stack's constructor. There's an overload with which you can set the stack's initial size.