Idiomatic C++ does not rely on manual memory management—it relies on deterministic memory management. Use unique_ptr, or shared_ptr when it makes sense (hint: it rarely does).
Of course the recommendation from Stroustrup & Sutter is to use the new smart pointers for everything but I think it will be a few years at least before most people can follow that advice.
Image* image = allocate_image(…);
…
deallocate_image(image);
↓
struct ImageDeleter {
void operator()(void* image) const {
deallocate_image(image);
}
};
unique_ptr<Image, ImageDeleter> image(allocate_image(…));
That certainly can’t work everywhere, but I have yet to be failed by it.scoped_ptr<Image> image(allocate_image(...));
std::unique_ptr<Image, void (*)(Image *)>(image, deallocate_image);