Alex Rivera | Logout

Why can't the runtime environment decide to apply delete or delete[] instead of the programmer?

Asked 2011-12-06T18:46:37.867
18

I've read that the delete[] operator is needed because the runtime environment does not keep information about if the allocated block is an array of objects that require destructor calls or not, but it does actually keep information about where in memory is the allocated block stored, and also, of course, the size of the block.
It would take just one more bit of meta data to remember if destructors need to be called on delete or not, so why not just do that?

I'm pretty sure there's a good explanation, I'm not questioning it, I just wish to know it.

Edit
Report

1 Answer

10

I think the reason is that C++ doesn't force you into anything you don't want. It would add extra metadata and if someone didn't use it, that extra overhead would be forced upon them, in contrast to the design goals of the C++ language.

When you want the capability you described, C++ does provide a way. It's called std::vector and you should nearly always prefer it, another sort of container, or a smart pointer over raw newand delete.

answered 2011-12-06T18:49:24.240

Your Answer