Which is more memory efficient and why between declaring the size of an array at run time using the 'new' keyword for dynamic memory allocation and using the method below:
#include <iostream>
using namespace std;
int main ()
{
int size;
cin >> size;
int a[size];
}
Dynamic Memory allocation using 'new' key word
#include <iostream>
using namespace std;
int main ()
{
int *array {nullptr};
int size;
cin >> size;
array = new int[size];
delete [] array;
}
int a[size];is not valid C++ (use VLA extension).int a[size];does not compile on MSVC: godbolt.org/z/xo13cW - as mentioned before, it is not standard C++, it uses an extension that some compilers supportstd::vectorover raw owning pointer.