静态数组VS.用C动态数组++ 11 [英] Static arrays VS. dynamic arrays in C++11

查看:217
本文介绍了静态数组VS.用C动态数组++ 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个已经在世界各地多次讨论一个非常古老的话题。但我目前有麻烦决定我应该在特定情况下使用哪种方法,而不是另一个静态和动态数组之间。其实,我woudn't已经使用C ++ 11,我会用静态数组。但我现在很困惑,因为有可能是与两个等价的利益。

I know that it's a very old debate that has already been discussed many times all over the world. But I'm currently having troubles deciding which method I should use rather than another between static and dynamic arrays in a particular case. Actually, I woudn't have used C++11, I would have used static arrays. But I'm now confused since there could be equivalent benefits with both.

解决方案一:

template<size_t N>
class Foo
{
    private:
        int array[N];

    public:
        // Some functions
}

解决方法二:

template<size_t N>
class Foo
{
    private:
        int* array;

    public:
        // Some functions
}

我不能碰巧选了,因为两人有自己的优势:

I can't happen to chose since the two have their own advantages:


  • 静态数组速度更快,我们不关心内存管理的。

  • 动态数组不只要内存不会被分配自重什么。在这之后,它们更方便的比静态阵列一起使用。但由于C ++ 11,我们可以有从移动语义,我们不能用静态数组使用极大的好处。

我不认为这是一个很好的解决办法,但我希望得到一些建议或只是知道你怎么想的一切。

I don't think there is one good solution, but I would like to get some advice or just to know what you think of all that.

推荐答案

我居然会用这取决于不以为然。如果你想使用translationtime不变,始终使用选择1或std ::阵列切勿使用选项2。你列出的一个优点,即动态数组权衡什么,直到分配,实际上是一个可怕的,巨大的劣势,和一个需要被指出了高度重视。

I will actually disagree with the "it depends". Never use option 2. If you want to use a translationtime constant, always use option 1 or std::array. The one advantage you listed, that dynamic arrays weigh nothing until allocated, is actually a horrible, huge disadvantage, and one that needs to be pointed out with great emphasis.

永远不要有有建筑的多个阶段的对象。永远不能。这应该是通过一些大的纹身致力于内存的规则。只是没有做到这一点。

Do not ever have objects that have more than one phase of construction. Never, ever. That should be a rule committed to memory through some large tattoo. Just never do it.

当你有僵尸对象不太活着呢,虽然不是很死或者,在管理其一生的复杂性呈指数级增长。你必须在每一个方法来检查它是否是完全活着,或仅pretending地活着。异常安全性需要你的析构函数特殊情况。而不是一个简单的结构和自动毁灭,你现在已经补充说,必须在N个不同的地方进行检查(#方法+析构函数)的要求。如果你检查编译器不关心。和其他工程师没有这个要求广播,所以他们可能不安全的方式调整code,使用变量不检查。现在所有这些方法都依赖于对象的状态多个行为,所以对象的每个用户需要知道会发生什么。 僵尸会毁了你(编码)的生活

When you have zombies objects that are not quite alive yet, though not quite dead either, the complexity in managing their lifetime grows exponentially. You have to check in every method whether it is fully alive, or only pretending to be alive. Exception safety requires special cases in your destructor. Instead of one simple construction and automatic destruction, you've now added requirements that must be checked at N different places (# methods + dtor). And the compiler doesn't care if you check. And other engineers won't have this requirement broadcast, so they may adjust your code in unsafe ways, using variables without checking. And now all these methods have multiple behaviors depending on the state of the object, so every user of the object needs to know what to expect. Zombies will ruin your (coding) life.

相反,如果你在程序中两个不同的自然寿命,使用两个不同的对象。但是,这意味着你有两个不同的状态在程序中,因此,你应该有一个状态机,具有只是一个对象,并使用这两种另一种状态,由一个异步事件分隔的一个状态。如果在两个点之间没有异步事件,如果他们都适合在一个函数范围,然后分离是人工,你应该做的单相结构。

Instead, if you have two different natural lifetimes in your program, use two different objects. But that means you have two different states in your program, so you should have a state machine, with one state having just one object and another state with both, separated by an asynchronous event. If there is no asynchronous event between the two points, if they all fit in one function scope, then the separation is artifical and you should be doing single phase construction.

其中一个转换时的大小应该转化为一个动态分配的唯一情况是,当大小的堆栈太大。这就到达内存优化,它应该始终使用内存和分析工具,看看什么是最好的评价。选项​​2将永远是最好(它采用的是赤裸裸的指针 - 让我们再次失去RAII和所有的自动清理和管理,增加不变量并使code更复杂,容易受他人易碎)。矢量(由位掩码建议)将是适当的第一个念头,虽然你可能不喜欢在时间上堆分配成本。其他选项可能会在应用程序中的图像静态空间。但同样,这些只能一旦你确定你有一个内存限制以及如何从那里应该由实际可测量的需要来确定考虑。

The only case where a translation time size should translate to a dynamic allocation is when the size is too large for the stack. This then gets to memory optimisation, and it should always be evaluated using memory and profiling tools to see what's best. Option 2 will never be best (it uses a naked pointer - so again we lose RAII and any automatic cleanup and management, adding invariants and making the code more complex and easily breakable by others). Vector (as suggested by bitmask) would be the appropriate first thought, though you may not like the heap allocation costs in time. Other options might be static space in your application's image. But again, these should only be considered once you've determined that you have a memory constraint and what to do from there should be determined by actual measurable needs.

这篇关于静态数组VS.用C动态数组++ 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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