Yes, you need to release the memory from new.
Alternatively with C++11, you can use smart pointer:
void work() {
std::unique_ptr<int[]> p{ new int[10] };
//some code....
// RAII will delete[] magically here
}
Alternatively, if you are using just few integers (10 in your case), that are known in compile time, you can do "normal" or static
array. e.g.
void work() {
/* static */ int p[10];
//some code....
}
Alternatively, use std::vector
:
void work() {
std::vector<int> p{10};
//some code....
// RAII will delete[] magically here
}
Alternatively, with C++11
if array size is known in compile time, use std::array
:
void work() {
std::array<int,10> p;
//some code....
// RAII will delete[] magically here
}