建议动态分配还是自动创建子系统?

发布于 2024-10-18 09:29:50 字数 1031 浏览 1 评论 0原文

我是一名业余 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 技术交流群。

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

发布评论

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

评论(3

夜灵血窟げ 2024-10-25 09:29:50

始终优先选择自动分配而不是动态分配。当您需要动态分配时,请确保其生命周期由自动分配的资源包装器(如智能指针)管理。

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.

走野 2024-10-25 09:29:50

在这种情况下,我认为一切都取决于规模。

您不想浪费堆栈空间,因此要么使用 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 put Application as a global variable outside main().

压抑⊿情绪 2024-10-25 09:29:50

在 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 your Application object on the heap -- new allocates memory dynamically at run time, where the allocation of auto 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 for main 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.

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