I think in this case, I'd avoid freeing and reallocating the array's storage. About the best you can hope for is that it'll end up nearly as efficient as just writing to all the elements in the array. In reality, it'll almost always be at least a little slower.
I probably wouldn't just write a loop either though. I'd probably use std::fill
or std::fill_n
to fill the array with 0
, or nullptr (etc.) depending on the type.