Is it possible to either create a coding standard or use of a library that can be proved to eliminate any memory management errors in C++?
I'm thinking of something like Java, it is just impossible to for example have dangling pointers in Java applications.
Is it possible to either create a coding standard or use of a library that can be proved to eliminate any memory management errors in C++?
Yes and no.
Even if you use a very strict standard, doing so will limit you to a very narrow subset of the C++ language. For example, the Power of Ten (Rules for Developing Safety-Critical Code) says that you should disable heap usage entirely. However that alone doesn't stop you from creating memory corruption.
I think if there were an exact answer to this question, the industry would've solved this decades ago, but here we are...
I don't believe that there is a definite way to make sure your code is totally safe, but there are best practices which will help you make sure there are as few problems as possible.
Here are some suggestions:
shared_ptr
, unique_ptr
etc. But course, you could still abuse these if you wanted to. (For example shared_ptr
will not help you if you have circular references...)valgrind
, which can help you discover problems and verify that your code is error-free.Even if you keep to any coding standard or best practice, errors can and will happen. Nobody guarantees that you will be safe. However, by keeping to these suggestions you can minimize the chance and impact of errors.
Is it possible to either create a coding standard or use of a library that can be proved to eliminate any memory management errors in C++?
No.
But the same is true for Java. While Java does not technically allow memory leaks, it does have them (and other resource leaks) in practice if you do not pay attention.
A classical example, known particularly well in the Android world, is when a collection of listener instances keeps growing the longer a program runs, because listeners forget to unregister themselves. This can quickly cause hundreds of MBs leaking in a GUI application when the listener is an instance of some window or view class that keeps itself references to big graphics. Since all of that memory is still reachable, garbage collection cannot clean it up.
The fact that you have not technically lost the pointer (it's still in the collection) does not help you at all. Quite the contrary; it's the cause of the leak because it prevents garbage collection.
In the same vein as above, while Java does not technically allow dangling pointers, similar bugs can cause you to access a pointer to some window or view object which is still in a valid memory area for your program but which should no longer exist and is no longer visible. While the pointer access itself does not cause any crash or problem, other kinds of errors or crashes (like a NullPointerException
) usually follow soon enough because the program logic is messed up.
So much for the bad news.
The good news is that both languages allow you to reduce memory-management problems if you follow simple guidelines. As far as C++ is concerned, this means:
std::vector
or std::set
) whenever you can.std::unique_ptr
.std::shared_ptr
.new
only if you implement some low-level container class because the existing standard container classes (likestd::vector
or std::set
) do not work for your use case. This should be an extremely rare case, though.There is also the Boehm garbage collector for C++, but I've never used it personally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With