堆栈与堆。一个加载的问题。 [英] stack vs. heap. a loaded question.

查看:57
本文介绍了堆栈与堆。一个加载的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在编写一个需要尽快运行的程序,但是在内存中保存了大量的数据(大约1GB)通常运行)。这是太多了

内存甚至考虑将大部分/全部放在堆栈上?这是不是很重要?


在决定如何分配许多数据结构时,我可能会考虑哪些因素?顺便说一句,该系统将包含一小撮非常大的数据结构(主要是矩阵和向量)。


从历史上看,我一直使用堆。这更多是我的C

背景的结果。但是,我现在正在编写一个系统,其中
将仅使用C ++和OO实践。因此,我觉得我有一个新的选择

来制作。艾米帮助使这个设计尽可能高效将非常感谢




谢谢,

dan elliott

Hello all,

I am writing a program which needs to run as quickly as possible, but holds
a lot of data in memory (around 1GB for a usual run). Is this too much
memory to even consider putting most/all of it on the stack? Does it
matter?

Any considerations I might take into account when deciding how to allocate
the many data structures? By the way, the system will consist of a handful
of very large data structures (primarily matrices and vectors).

Historically, I have always used the heap. This was more a result of my C
background than anything else. However, I am now writing a system which
will use only C++ and OO practices. Therefore, I feel I have a new choice
to make. Amy assistance to make this design as efficient as possible would
be greatly appreciated.

Thank you,

dan elliott

推荐答案

Dan Elliott napisa3:
Dan Elliott napisa3:
大家好,

我写的是程序需要尽可能快地运行,但在内存中保存了大量数据(通常运行时大约1GB)。即使考虑将大部分/全部放在堆栈上,这是否太多内存?是否重要?

在决定如何分配许多数据结构时,我可能会考虑哪些因素?顺便说一下,系统将包含一些非常大的数据结构(主要是矩阵和向量)。

从历史上看,我一直使用堆。这更多是我的C
背景的结果。但是,我现在正在编写一个只使用C ++和OO实践的系统。因此,我觉得我有一个新的选择。非常有助于使这个设计尽可能高效。
Hello all,

I am writing a program which needs to run as quickly as possible, but holds
a lot of data in memory (around 1GB for a usual run). Is this too much
memory to even consider putting most/all of it on the stack? Does it
matter?

Any considerations I might take into account when deciding how to allocate
the many data structures? By the way, the system will consist of a handful
of very large data structures (primarily matrices and vectors).

Historically, I have always used the heap. This was more a result of my C
background than anything else. However, I am now writing a system which
will use only C++ and OO practices. Therefore, I feel I have a new choice
to make. Amy assistance to make this design as efficient as possible would
be greatly appreciated.




C ++没有堆或堆栈的概念(嗯,并不意味着你无论如何都使用了b $ b,但是< OT>通常,通过

new或malloc分配的数据最终会在堆上。堆栈用于

本地对象,不应该太大。堆栈是

通常限制在最多几MB,通常更少。 < / OT>


因此我建议你从堆中分配:


void foo(){

bigmatrix Matrices [1000]; //在堆栈上分配,禁止使用

}


vod goo(){

bigmatrix * Matrices = new bigmatrix [1000]; //在堆上分配,确定


//当你完成时不要忘记删除[] Matrices

}


我怀疑你有自定义矩阵或矢量类?如果没有,

您可能想要使用std :: vector<>对于你的数据。


HTH,

- J.



C++ has no concept of heap or stack (well, not in the meaning you
have used, anyway), but <OT> usually the data allocated via
new or malloc will end up on the heap. The stack is used for
local objects which should not be too large. The stack is
usually limited to at most a few MB, often less. </OT>

Therefore I suggest you allocate from the heap:

void foo() {
bigmatrix Matrices[1000]; // allocates on the stack, no-go
}

vod goo() {
bigmatrix* Matrices =new bigmatrix[1000]; // allocates on the heap, ok

// don''t forget to "delete[] Matrices", when you''re done
}

I suspect you have a custom matrix or vector class? If not,
you might want to use std::vector<> for your data.

HTH,
- J.


Dan Elliott写道:
Dan Elliott wrote:
我正在编写一个需要尽可能快地运行的程序
但是在内存中保存了大量数据(通常运行大约1GB)。
这是太多的内存<甚至考虑将大部分/全部放在堆栈上?


你会如何把它放在堆栈上?

这有关系吗?


如果您有很多小物件,请使用自动存储[堆栈]。

为大型物品使用自动存储[> 1GByte]

可能不是一个好主意。

在决定如何分配许多数据结构时,我可能会考虑什么?顺便说一句,该系统将包含一些非常大的数据结构(主要是矩阵和向量)。


除非你打算使用可变大小的数组

来表示向量和矩阵,否则你将免费分配内存

无论如何都要存储这些对象。

从历史上看,我一直都在使用堆。
这更多是我的C背景而不是其他任何东西。
但是,我现在正在写一个只使用C ++和OO实践的系统。
因此,我觉得我有一个新的选择。


我不这么认为。

我觉得你很困惑。

所有的矢量和矩阵类对于大型物体

从免费存储空间分配内存。

任何有助于使此设计尽可能高效的帮助
将不胜感激。
I am writing a program which needs to run as quickly as possible
but holds a lot of data in memory (around 1GB for a usual run).
Is this too much memory
to even consider putting most/all of it on the stack?
How would you "put it on the stack"?
Does it matter?
Use automatic storage [the stack] if you have lots of small objects.
Using automatic storage for large objects [> 1GByte]
might not be such a good idea.
Any considerations I might take into account
when deciding how to allocate the many data structures?
By the way, the system will consist of a handful
of very large data structures (primarily matrices and vectors).
Unless you are planning to use variable size arrays
to represent vectors and matrices, you will be allocating memory
from free storage for these objects anyway.
Historically, I have always used the heap.
This was more a result of my C background than anything else.
However, I am now writing a system which will use only C++ and OO practices.
Therefore, I feel I have a new choice to make.
I don''t think so.
I think that you are confused.
All of the vector and matrix classes for large objects
allocate memory from free storage.
Any assistance to make this design as efficient as possible
would be greatly appreciated.




看看

Scalar,Vector,Matrix和Tensor类库

http://www.netwood.net/~edwin/svmtl/


然后访问

面向对象的数字页面
http://www.oonumerics.org/oon/


2004年11月17日星期三22:02:58 +0100 ,Jacek Dziedzic写道:
On Wed, 17 Nov 2004 22:02:58 +0100, Jacek Dziedzic wrote:
Dan Elliott
Dan Elliott
大家好,

我正在编写一个程序,需要尽快运行可能,但在内存中保存了大量数据(通常运行大约1GB)。即使考虑将大部分/全部放在堆栈上,这是否太多内存?是否重要?

在决定如何分配许多数据结构时,我可能会考虑哪些因素?顺便说一下,系统将包含一些非常大的数据结构(主要是矩阵和向量)。

从历史上看,我一直使用堆。这更多是我的C
背景的结果。但是,我现在正在编写一个只使用C ++和OO实践的系统。因此,我觉得我有一个新的选择。 Amy帮助使这个设计尽可能高效。
非常感谢。
Hello all,

I am writing a program which needs to run as quickly as possible, but holds
a lot of data in memory (around 1GB for a usual run). Is this too much
memory to even consider putting most/all of it on the stack? Does it
matter?

Any considerations I might take into account when deciding how to allocate
the many data structures? By the way, the system will consist of a handful
of very large data structures (primarily matrices and vectors).

Historically, I have always used the heap. This was more a result of my C
background than anything else. However, I am now writing a system which
will use only C++ and OO practices. Therefore, I feel I have a new choice
to make. Amy assistance to make this design as efficient as possible would
be greatly appreciated.



C ++没有堆或堆栈的概念(好吧,不是你的意思) >已经使用过,但是< OT>通常,通过
new或malloc分配的数据最终会堆在堆上。堆栈用于不应该太大的本地对象。堆栈通常限制在最多几MB,通常更少。 < / OT>

因此我建议你从堆中分配:

void foo(){
bigmatrix Matrices [1000]; //在堆栈上分配,禁止


vod goo(){
bigmatrix * Matrices = new bigmatrix [1000]; //在堆上分配,确定
//不要忘记删除[] Matrices,当你完成时
}

>我怀疑你有自定义矩阵或矢量类?如果没有,
你可能想使用std :: vector<>对于您的数据。

HTH,
- J。



C++ has no concept of heap or stack (well, not in the meaning you
have used, anyway), but <OT> usually the data allocated via
new or malloc will end up on the heap. The stack is used for
local objects which should not be too large. The stack is
usually limited to at most a few MB, often less. </OT>

Therefore I suggest you allocate from the heap:

void foo() {
bigmatrix Matrices[1000]; // allocates on the stack, no-go
}

vod goo() {
bigmatrix* Matrices =new bigmatrix[1000]; // allocates on the heap, ok

// don''t forget to "delete[] Matrices", when you''re done
}

I suspect you have a custom matrix or vector class? If not,
you might want to use std::vector<> for your data.

HTH,
- J.




感谢您的回复。因此,作为一般规则,大型对象应该在堆上分配
。以什么方式是堆叠。尺寸有限

表示堆积不是?对不起,如果这是我应该有的东西

作为本科生学习。


这个问题在我读到关于
的时候变得特别突出。
uBLAS库。他们提到一个特定的数据结构将会在自动存储上分配矩阵(我的术语中的堆栈),这比
优于免费商店。


再次感谢您让我在系统设计方面的待遇更加明亮。


- dan



Thank you for the response. So, as a general rule, large objects should
be allocated on the "heap?" In what way is the "stack" limited in size
that the "heap" is not? I sorry if this is something I should have
learned as an undergrad.

This question became especially salient in my mind when reading about the
uBLAS library. They mention that a particular data structure will
allocate matrices on automatic storage (stack in my terminology) which
is better than free store.

Thanks again for making my stay in system design hell a little brighter.

- dan


这篇关于堆栈与堆。一个加载的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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