Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it take to write memory safe C++ 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++?

I'm thinking of something like Java, it is just impossible to for example have dangling pointers in Java applications.

like image 443
Jesus H Avatar asked Jan 12 '17 15:01

Jesus H


2 Answers

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:

  • As mentioned earlier, disallowing heap usage entirely might help you get rid of all the memory management problems, but it doesn't solve the problem completely because it doesn't save you from eg. stray pointer writes.
  • I recommend you read the about The Rule of Three, Five and Zero which explain some of the stuff you need to take care of.
  • Instead of managing memory on your own, use smart pointers like 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...)
  • Use memory checker tools like 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.

like image 153
Venemo Avatar answered Oct 16 '22 22:10

Venemo


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:

  • Use standard collections (like std::vector or std::set) whenever you can.
  • Make dynamic allocation your second choice. The first choice should always be to create a local object.
  • If you must use dynamic allocation, use std::unique_ptr.
  • If all else fails, consider std::shared_ptr.
  • Use a bare 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.

like image 27
Christian Hackl Avatar answered Oct 16 '22 22:10

Christian Hackl