you want to use std::vector
in most cases.
std::vector<int> array;
array.resize(someSize);
But if you insist on using new
, then you have do to a bit more work than you do in Java.
int *array;
array = new int[someSize];
// then, later when you're done with array
delete [] array;
No c++ runtimes come with garbage collection by default, so the delete[]
is required to avoid leaking memory. You can get the best of both worlds using a smart pointer type, but really, just use std::vector
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…