I really disagree with "it depends." Never use parameter 2. If you want to use the translation time constant, always use parameter 1 or std :: array. The only advantage that you indicated is that dynamic arrays do not weigh anything before allocation, in fact this is a terrible, huge drawback and one that needs special attention.
Never have objects with more than one phase of construction . Never never. This should be the rule assigned to the memory through a large tattoo. Just never do that.
When you have zombie objects that are not yet completely alive, although not quite dead, the difficulty in managing their life time grows exponentially. You must check each method to see if it is completely alive, or just pretend to be alive. Exception security requires special cases in your destructor. Instead of one simple construction and automatic destruction, you added requirements that should be checked in N different places (# methods + dtor). And the compiler doesn't care if you check. And other engineers will not pass this requirement, so they can configure your code in unsafe ways using variables without checking. And now all these methods have multiple behavior depending on the state of the object, so every user of the object must know what to expect. Zombies will ruin your (encoding) life.
Instead, if you have two different natural lifetimes in your program, use two different objects. But this means that you have two different states in your program, so you must have a state machine, with one state having only one object and another state with both, separated by an asynchronous event. If there is no asynchronous event between two points, if they all fit into the same function area, then the separation is artificial, and you must perform a single-phase construction.
The only case where the size of the translation time should translate into dynamic allocation is when the size is too large for the stack. This then leads to memory optimization, and it should always be evaluated using memory and profiling tools to see which is best. Option 2 will never be the best (it uses a bare pointer - so we lose RAII and any automatic cleaning and control, adding invariants and making the code more complex and easily breaking by others). A vector (as suggested by the bitmask) would be a good first thought, although you may not like the cost of allocating the heap over time. Other options may be static space in the image of your application. But again, they should only be considered after you have determined that you have a memory limit, and what to do next should be determined by real, measurable needs.
source share