我可以使用块来管理C ++中的内存消耗吗? [英] Can I use blocks to manage memory consumtion in C++?

查看:84
本文介绍了我可以使用块来管理C ++中的内存消耗吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C ++程序中节省一些内存,我想知道是否可以将块用作变量的作用域(如Perl)。假设我有一个巨大对象,该对象执行一些计算并给出结果,这样做有意义吗?

  InputType输入; 
ResultType结果;

{
//代码块
MyHugeObject mho;
结果= mho.superHeavyProcessing();
}

/ *
我的其他代码...
* /

退出块时可以期望物体被破坏吗?

解决方案

是的,



一旦变量超出范围,就会调用析构函数,并且应该释放堆分配的内存。 / p>

I'm trying to gain some memory saving in a C++ program and I want to know if I can use blocks as a scope for variables (as in Perl). Let's say I have a huge object that performs some computations and gives a result, does it makes sense to do:

InputType  input;
ResultType result;

{
    // Block of code
    MyHugeObject mho;
    result = mho.superHeavyProcessing();
}

/*
   My other code ...
*/

Can I expect the object to be destroyed when exiting the block?

解决方案

Yes, you can.

The destructor will be called as soon as the variable falls out of scope and it should release the heap-allocated memory.

这篇关于我可以使用块来管理C ++中的内存消耗吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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