Boost logo

Boost :

From: Paul Giaccone (paulg_at_[hidden])
Date: 2006-03-13 10:20:40


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));

instead of:

    boost::shared_array<double> foo(new double[num]);

(For those without sharp eyes, that's parentheses instead of square
brackets.)

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:

    boost::shared_array<double> foo(num);

Paul


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk