Re:Why should I have written ZeroMQ in C, not C++
by evincarofautumn on Thursday May 10, 2012 @11:24AM
(#3953983)
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).
[ Parent
| on HN ]
Re:Why should I have written ZeroMQ in C, not C++
by cageface on Thursday May 10, 2012 @11:28AM
(#3953999)
In theory you have every single leakable resource wrapped up in a RAII container or a smart pointer. In reality you're almost always dealing with raw pointers coming from a lower level C API or old C++ code or some kind of object handle you haven't wrapped yet.
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.
Re:Why should I have written ZeroMQ in C, not C++
by evincarofautumn on Thursday May 10, 2012 @12:13PM
(#3954259)
Typically what I do in that situation is lightly wrap the API. For instance:
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.You can reduce verbosity of the code by passing destructor function directly to unique_ptr constructor:
std::unique_ptr<Image, void (*)(Image *)>(image, deallocate_image);
Re:Why should I have written ZeroMQ in C, not C++
by evincarofautumn on Friday May 11, 2012 @12:40PM
(#3960065)
Cool, thanks for the tip. I meant to offer an example for the general case.
Re:Why should I have written ZeroMQ in C, not C++
by jongraehl on Thursday May 10, 2012 @07:18PM
(#3956672)
People who don't do this are just asking for it.
Re:Why should I have written ZeroMQ in C, not C++
by malkia on Thursday May 10, 2012 @11:27PM
(#3957460)
That's a lot of boilerplate code. Okay there are macros to wrap such things, but I guess it's things like this that would make the future reader of the code puzzled.
Re:Why should I have written ZeroMQ in C, not C++
by cbsmith on Friday May 11, 2012 @05:17AM
(#3958359)
jmq & basman provide examples of how to do it succinctly.
Re:Why should I have written ZeroMQ in C, not C++
by basman on Thursday May 10, 2012 @11:49PM
(#3957544)
Or, perhaps easier:
scoped_ptr<Image> image(allocate_image(...));
Re:Why should I have written ZeroMQ in C, not C++
by ajross on Thursday May 10, 2012 @11:35AM
(#3954051)
Sure. And using deep-thrown (i.e. thrown across module/library boundaries -- often exceptions are local tricks used within tightly coupled code) exceptions in an application where you're dealing with such pointers would be bad design.