c++cpointersmemory-leaksbuffer-overrun

How do you program safely outside of a managed code environment?


If you are someone who programs in C or C++, without the managed-language benefits of memory management, type checking or buffer overrun protection, using pointer arithmetic, how do you make sure that your programs are safe? Do you use a lot of unit tests, or are you just a cautious coder? Do you have other methods?


Solution

  • All of the above. I use:

    1. A lot of caution
    2. Smart Pointers as much as possible
    3. Data structures which have been tested, a lot of standard library
    4. Unit tests all the time
    5. Memory validation tools like MemValidator and AppVerifier
    6. Pray every night it doesn't crash on customer site.

    Actually, I am just exaggerating. Its not too bad and its actually not too hard to keep control of resources if you structure your code properly.

    Interesting note. I have a large application which uses DCOM and has managed and unmanaged modules. The unmanaged modules generally are harder to debug during development, but perform very well at the customer site because of the many tests run on it. The managed modules sometimes suffer from bad code because the garbage collector is so flexible, programmers get lazy in checking resource usage.