Hacker News new | ask | show | jobs
by loup-vaillant 4316 days ago
Ordinary C++ with good old RAII do use something akin to garbage collection: many classes, when constructed, allocate memory on the heap, then free that memory when they fall out of scope.

Malloc() and free() aren't exactly predictable. If you really want guarantees, you need to write your own custom allocator, and if you don't need those guarantees, garbage collection is probably enough for your purposes.

Basically, if you're using C++ without real manual management, with custom allocators, pools, and all, you probably didn't need C++ in the first place.

1 comments

RAII is a form of automatic resource management, it's true. It is, however, a deterministic form of automatic resource management.

This is not to say that the underlying allocator is necessarily deterministic, but you're making a false dilemma here by putting it in opposition to making decisions about custom allocators and pool allocators. You can use RAII with custom allocators. You can use RAII with memory pools.

And that's where C++ really does shine. It's a niche that hasn't even really been attempted much, let alone that it's been surpassed in.