Why we use new operator and pointer for dynamic memory allocation when we can just take user defined size and allocate array as arr[size]? [duplicate]

  c++, c++14, dynamic-memory-allocation

Few days back i had started learning c++ and recently i had came across a topic dynamic memory allocation.

Where i learned that if i have to allocate elements in an array then things will work something like:

int sizeofarray;
cin>>sizeofarray;
int *nameofdynamicarray;
nameofdynamicarray=new int[sizeofarray];

for(int i=0;i<sizeofarray;i++)
{
cin>>nameofdynamicarray[i];
}

My question is:

int main(){

int sizeofarray;
cin>>sizeofarray;

int nameofarray[sizeofarray];
    for(int i=0;i<sizeofarray;i++)
    {
    cin>>nameofarray[i];
    }

return 0;
}

Why i this method is not called dynamic memory allocation and why to use that com

plicated with new operator and pointer stuff ?

Source: Windows Questions C++

LEAVE A COMMENT