0

I know that, in C++, there is 2 kind of arrays : statics and dynamics arrays. But I have a question :

Why would we use static arrays rather than dynamics arrays in some cases whereas, it seems to me, we can do more things with dynamics arrays than with statics arrays ?

Why would we use dynamics arrays in all cases ?

6
  • 2
    One reason is that they require a dynamic memory allocation, which may be considered too costly. Also, they have to be de-allocated manually, and this is easy to mess up. The latter problem can be avoided by using e.g. std::vector. Commented Feb 8, 2014 at 15:22
  • Dynamic arrays generally carry more overhead (an extra level of indirection, at least). Commented Feb 8, 2014 at 15:22
  • 2
    stackoverflow.com/questions/15062767/… Commented Feb 8, 2014 at 15:22
  • @juanchopanza 'which may be considered too costly' or simply aren't available for the platform (which is one of my daily bread & butter) ... Commented Feb 8, 2014 at 15:28
  • @πάνταῥεῖ I guess it would be too costly to use another platform then :-) Commented Feb 8, 2014 at 15:32

2 Answers 2

3

Why would we use static arrays rather than dynamics arrays in some cases whereas, it seems to me, we can do more things with dynamics arrays than with statics arrays ?

Assuming that with static arrays you mean C-style arrays or std::array and with dynamic arrays you mean std::vector, because:

  1. static arrays can be optimized by the compiler, since the size in known at compile time.
  2. dynamic arrays make use of dynamic memory allocation, which can be expensive.
Sign up to request clarification or add additional context in comments.

Comments

0

FYI, in Embedded Systems, memory is restricted (small in size).

Static arrays allow one to constrain any sequence without running into the problems of fragmentation.

Also, a constant static array can be placed into Read Only Memory, freeing up read/write memory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.