c++memory-managementmemory-safety

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.


Solution

  • 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:

    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.