建议动态分配还是自动创建子系统?
我是一名业余 C++ 程序员,目前正在开发一个游戏(使用 Ogre3D),我有一个关于我的主类的内存分配的问题。
我读了很多关于内存分配、在堆栈上自动分配和在堆上动态分配以及它们的差异(性能、有限的堆栈大小)的文章。我仍然不确定我的主类(应用程序)和其他一些“工厂”类(由应用程序类的单个实例创建)使用什么,它们在整个执行过程中都存在一个实例。
下面是布局的简化片段:
int main()
{
// like this (automatic)
Application app;
app.create(); // initializing
app.run(); // runs the game-loop
// or like this (dynamic)
Application* app;
app = new Application();
app->create();
app->run();
return(0); // only reached after exiting game
}
class Application
{
public:
Application(); // ctor
~Application(); // dtor
// like this, using 'new' in ctor and 'delete' in dtor (dynamic)
SceneManager* sceneManager_; // a factory for handling scene objects
DebugManager* debugManager_; // a factory for handling debugging objects
// or like this (automatic)
SceneManager sceneManager_;
DebugManager debugManager_;
};
在堆栈上还是在堆上分配内存(对于应用程序类和工厂类)更好?依据什么论据?
提前致谢!
I am a hobbyist C++ programmer and currently working on a game (using Ogre3D) and I have a question regarding the memory allocation for my main classes.
I have read a lot on memory allocation, allocating automatically on the stack and dynamically on the heap, and their differences (performance, limited stack size). Still I am not sure what to use for my main class (Application) and some other 'factory' classes (created by a single instance of the Application class), which will all have a single instance existing throughout the entire execution.
Below is a simplified snippet of the layout:
int main()
{
// like this (automatic)
Application app;
app.create(); // initializing
app.run(); // runs the game-loop
// or like this (dynamic)
Application* app;
app = new Application();
app->create();
app->run();
return(0); // only reached after exiting game
}
class Application
{
public:
Application(); // ctor
~Application(); // dtor
// like this, using 'new' in ctor and 'delete' in dtor (dynamic)
SceneManager* sceneManager_; // a factory for handling scene objects
DebugManager* debugManager_; // a factory for handling debugging objects
// or like this (automatic)
SceneManager sceneManager_;
DebugManager debugManager_;
};
Is it better to allocate memory on the stack or on the heap (both for the Application class and the factory classes)? And by what arguments?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
始终优先选择自动分配而不是动态分配。当您需要动态分配时,请确保其生命周期由自动分配的资源包装器(如智能指针)管理。
Always prefer automatic allocation over dynamic allocation. And when you need dynamic allocation, make sure its lifetime is managed by automatically allocated resource wrappers, like smart pointers.
在这种情况下,我认为一切都取决于规模。
您不想浪费堆栈空间,因此要么使用
new
进行动态分配,要么将Application
作为全局变量放在main()
之外。In this situation I think it all comes down to size.
You don't want to waste stack space, so either use dynamic allocation with
new
or putApplication
as a global variable outsidemain()
.在 C++ 中,问题相当复杂,但通常无法避免在堆上分配。例如,您的
new
操作正在堆上分配您的Application
对象 -new
在运行时动态分配内存,其中分配 < code>auto 内存在编译时确定。 (当然,它实际上是在运行时分配的——但是它是在启动代码根据编译的分配为main
创建堆栈时分配的。)现在,为什么您想要避免在堆上分配吗?这可能是因为堆大小有限,但对于现代机器来说,这很少是问题,即使在手持设备中也是如此。然而,堆栈空间可能是有限的。所以这就支持堆。
当然,
auto
内存不会“泄漏”——但是在主内存(或在文件范围内,尽管这是静态的)分配的内存不会被释放,所以人们几乎可以声称它“自动” ”泄露。我认为这里的基本问题实际上是“为什么你不在堆上分配?”主要原因通常是为了避免内存泄漏,但是谨慎使用 new/delete 可以保护您免受这种情况的影响 - 并且鉴于动态分配在库等中的普遍存在,您不能停止思考即使您设法避免动态分配,也要保持良好的内存卫生。
In C++, the question is rather more complicated, but in general you can't avoid allocating on the heap. For example, your
new
operation is allocating yourApplication
object on the heap --new
allocates memory dynamically at run time, where the allocation ofauto
memory is determined at compile time. (It's actually allocated at run time, of course -- but it's allocated as the startup code creates the stack formain
according to the compiled-in allocations.)Now, why would you want to avoid allocating on the heap? It might be because of limited heap sizes, but with modern machines that's rarely a problem, even in hand-held devices. Stack space might well be limited, however. So that argues for heap.
Of course,
auto
memory doesn't "leak" -- but memory allocated in the main (or in file scope, although that's static) doesn't get freed, so one could almost claim it "automatically" leaked.I think the essential question here is really "why wouldn't you allocate on the heap?" The main reason is usually to avoid memory leaks, but care in using
new/delete
can protect you from that -- and given the ubiquity of dynamic allocation in libraries and such, you can't stop thinking about good memory hygiene even if you contrive to avoid dynamic allocation.