Well, I searched on the internet about dynamically extending an array and found that it's not possible in C++. I have to use either malloc, realloc of C version or std::vector in C++. But the code below seems to work just fine.
#include<iostream>
using namespace std;
int main() {
int len = 10;
int *p = new int[len];
for (int i = 0; i < len; i++){
p[i] = i + 1;
}
*(p + len) = 1000; // extend here.
len++;
for (int i = 0; i < len; i++){
cout << p[i] << endl;
}
return 0;
}
Well, if I use this approach in big programs, is there any problem? why people said that it is not possible? If it's because the extended area might be already filled up with other information or anything else? I just want to know if the above approach is right. If not, then exactly why?
std::vector.std::vector.std::vector?