I stumbled upon a quiz that involved array declaration with different sizes. The first thing that came to my mind is that I would need to use dynamic allocation with the new
command, like this:
while(T--) {
int N;
cin >> N;
int *array = new int[N];
// Do something with 'array'
delete[] array;
}
However, I saw that one of the solutions allowed the following case:
while(T--) {
int N;
cin >> N;
int array[N];
// Do something with 'array'
}
After a bit of research I read that g++ allows this, but it kept me thinking, in which cases is it then necessary to use dynamic allocation? Or is it that the compiler translates this as dynamic allocation?
The delete function is included. Note, however, that the question in here is not about memory leaks.
std::vector
instead (std::vector<int> array(N);
).new OBJ
directly.