释放分配的内存

发布于 2024-09-10 01:04:31 字数 296 浏览 2 评论 0原文

这是好的做法吗?或者我应该用函数替换 {} 之间的代码块?它可以重用(我承认),但我这样做的唯一动机是释放 colsum 因为它巨大并且不是必需的,以便我可以释放分配的内存。

 vector<double> C;
 {
  vector<double> colsum;
  A.col_sum(colsum);
  C = At*colsum;
 }
 doSomething(C);

Is this good practice? Or should I just replace the code block between { and } with a function? It could be reusable (I admit) but my only motivation for doing this is to deallocate colsum since it's huge and not required so that I can free the allocated memory.

 vector<double> C;
 {
  vector<double> colsum;
  A.col_sum(colsum);
  C = At*colsum;
 }
 doSomething(C);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

智商已欠费 2024-09-17 01:04:31

在我的书中,使用括号来确定自动变量的范围是很好的,但通常如果您发现自己经常这样做,尤其是在同一个函数中多次这样做,那么您的函数可能正在做太多不同的事情,应该被分解。

Using brackets to scope automatic variables is fine in my book, but generally if you find yourself doing it a lot, especially multiple times in the same function, your function is probably doing too many different things and should be broken up.

顾北清歌寒 2024-09-17 01:04:31

矢量的数据始终是动态分配的。堆栈中仅存储簿记数据。即使不是,堆栈内存分配本质上也是免费的。在大多数体系结构上,从堆栈中释放只是更改寄存器的值。

编辑

关于动态释放,它必须在某一点或另一点释放(特别是在函数末尾)。保留分配的内存实际上并不会丢失任何东西,直到您想要分配更多内存但内存不足为止。在实际遇到问题之前,释放发生的确切时间真的值得关注吗?

/编辑

但这有什么意义呢?看来您确实过早地关注优化。

如果您想重构代码,请为了清晰起见,而不是为了性能。

The data for vectors is always dynamically allocated. Only the bookkeeping data is stored on the stack. Even if it weren't, stack memory allocation is essentially free. Deallocating from the stack is just changing the value of a register on most architectures.

EDIT

Regarding the dynamic deallocation, it will have to be deallocated at one point or another (particularly the end of the function). You're not actually losing anything by leaving the memory allocated until you want to allocate more and there isn't enough. Is the precise timing of when that deallocation occurs really something to be concerned about before you actually run into some problem?

/EDIT

But what's the point? It really seems like you're prematurely concerning yourself with optimization.

If you want to refactor your code, do it for the sake of clarity, not performance.

鲸落 2024-09-17 01:04:31

Vector不在堆栈上存储内存。那里只存储矢量对象本身,并不是很大。像这样确定范围确实会强制进行破坏,从而释放它分配的内存。

另外,我不确定 ISO 中是否指定实现必须从堆栈中弹出子范围变量。

Vector doesn't store memory on the stack. Only the vector object itself is stored there, which is not very large. Scoping it like this does force a destruction which will free the memory it has allocated.

Also, I'm not sure that it's specified in the ISO that an implementation must pop a subscope variable off of the stack.

铜锣湾横着走 2024-09-17 01:04:31

正如其他人指出的那样,向量内存不是在堆栈中分配的。如果您想尽早释放该内存,常见的习惯用法是:

vector<double> C;
vector<double> colsum;
A.col_sum(colsum);
C = At*colsum;
    std::vector<double>().swap(colsum);
doSomething(C);

这将创建一个临时向量并将内容与您的大向量交换。在指令结束时,临时内存将被销毁并释放内存。您将留下一个空向量。

请注意,colsum.resize(0)colsum.clear() 不需要释放可用内存,并且在许多情况下,它们不会假设如果向量以前增长到过这个大小,很可能它会再次增长。

As other have pointed out, the vector memory is not allocated in the stack. If you want to free that memory early on, the common idiom is:

vector<double> C;
vector<double> colsum;
A.col_sum(colsum);
C = At*colsum;
    std::vector<double>().swap(colsum);
doSomething(C);

That will create a temporary vector and swap the contents with your large vector. At the end of the instruction the temporary will be destroyed and the memory released. You will be left with an empty vector.

Note that colsum.resize(0) and colsum.clear() don't need to free the available memory, and in many cases they won't assuming that if the vector grew to that size before, chances are that it will do it again.

累赘 2024-09-17 01:04:31

如果内部代码要在其他地方重用,请将其分离成一个函数。如果内部代码被频繁调用(比如在循环中),那么它可能需要重构,以便向量不会在循环中不断创建和销毁。否则,我不认为按照你的建议去做是不好的做法。

If the inner code will be reused elsewhere, separate it into a function. If the inner code is called frequently (like in a loop), then it probably needs to be refactored so that the vector isn't being constantly created and destroyed in a loop. Otherwise, I don't think it is bad practice to do what you've suggested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文