[shared_array] Initialisation - could this subtle user bug be avoided?
I had some code in my program that created and initialised a
shared_array. When the shared_array was deleted, the program crashed
with an exception that suggested I had written beyond the end of the array.
I realised that the problem was I had written:
boost::shared_array<double> foo(new double(num));
boost::shared_array<double> foo(new double[num]);
(For those without sharp eyes, that's parentheses instead of square
The compiler was perfectly happy with this, of course, because new
double(num) is perfectly good syntax.
Is there any way that this bug could be caught at compile time? Perhaps
the solution might be to provide a separate constructor (a convenience
function) that took only a "size" parameter and did the "new" for you: