[shared_array] Initialisation - could this subtle user bug be avoided?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[shared_array] Initialisation - could this subtle user bug be avoided?

Paul Giaccone
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


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost