Declare large array on Stack

后端 未结 4 1064
我寻月下人不归
我寻月下人不归 2020-12-03 15:45

I am using Dev C++ to write a simulation program. For it, I need to declare a single dimensional array with the data type double. It contains 4200000

4条回答
  •  猫巷女王i
    2020-12-03 16:25

    Yes, you can declare this array on the stack (with a little extra work), but it is not wise.

    There is no justifiable reason why the array has to live on the stack.

    The overhead of dynamically allocating a single array once is neglegible (you could say "zero"), and a smart pointer will safely take care of not leaking memory, if that is your concern.
    Stack allocated memory is not in any way different from heap allocated memory (apart from some caching effects for small objects, but these do not apply here).

    Insofar, just don't do it.

    If you insist that you must allocate the array on the stack, you will need to reserve 32 megabytes of stack space first (preferrably a bit more). For that, using Dev-C++ (which presumes Windows+MingW) you will either need to set the reserved stack size for your executable using compiler flags such as -Wl,--stack,34000000 (this reserves somewhat more than 32MiB), or create a thread (which lets you specify a reserved stack size for that thread).
    But really, again, just don't do that. There's nothing wrong with allocating a huge array dynamically.

提交回复
热议问题