0

Is it true that dynamic memory allocation : int* ptr = new int[3] needs more RAM than static allocation: int ptr[3], because of 4 byte more to store pointer?

  • No, it's not true. It needs more RAM for a completely different reason. – Sam Varshavchik May 02 '20 at 13:08
  • Yes, but it's even worse than that because there is additional overhead in the heap data structure that you allocate memory from. Who knows, at this rate you may soon run out of memory. Basically the **important** difference between static allocation and dynamic allocation is that they behave differently. Choose the correct one for the behaviour you need not for trivial differences in memory consumption. – john May 02 '20 at 13:09

2 Answers2

0

Basically there is a difference in performance,dynamic allocation use the heap (so it as to ask OS to allocate that memory as soon as the program is calling new operator) static allocation inside a function (under some circumstances) use the stack (a pre-defined memory) that is more fast to be allocated at runtime.

SeventhSon84
  • 364
  • 1
  • 4
0

It depens a lot on the situation you're in.

The dynamic allocation uses the Heap so it has to ask for it to the OS when you use new. The static allocation uses the Stack so the memory "request" is faster than dynamic allocation being allocated as soon as the program starts.

If you need a small array and you use it in simple operations, for exercise purpose a static allocation is more than enough.

See: C/C++ performance of static arrays vs dynamic arrays