0

I understand the basic concept of Static Arrays and dynamic arrays. The main difference between these two are one(Static Array) allocates memory at compile time, and the other at runtime. However, we can create static array whose size would be the user entered value.

int value ;
cin >> value  ;

int array [value] ;
int i = 0 ;
while(i < value)
{
    cin >> array[i] ;
    i ++ ; 
}

So the program doesn't know the size of this array, until runtime. So What's the actual fundamental difference between static and dynamic arrays then, if static array can also be made to allocate space on runtime??

Also, talking about the resizability of the array. To expand a dynamic array, we can delete the previous array after copying it's elements to a new double sized dynamic array. We can do the same with Static Arrays too right? [However, the original array can not be deleted in this case, and uses redundant stack space. IS this the only difference between S.Array and D.Array then]

Jarod42
  • 203,559
  • 14
  • 181
  • 302

2 Answers2

0

There are compiler extensions called "Variable Length Array" as part C99 standards, which allows this declaration.

The C99 standard are not officially supported as part of C++ Language.

The code above will not work in MSVS (Microsoft Visual Studio) as it doesn't support C99, but will be successfully compiled by gcc and clang

Daksh Gupta
  • 7,554
  • 2
  • 25
  • 36
0

Static arrays get stored on the stack, but dynamic arrays are stored on the heap. When something is stored on the stack, the moment it goes out of scope, it is disposed of. Examples are ints, chars, etc. But, if it is created on the heap, you have to ensure garbage collection does its job when it goes out of scope. If you don't clean up and de-allocate memory properly, you can get memory leaks.

Elle
  • 17
  • 2
  • 5