What should I be cautious to not do while programming to not get these errors? |
Writing outside of array bounds. Reading outside of array bounds. Double deletes. Deleting non-newed pointers. Uninitialized variables (particularly deadly when combined with optimized switches).
Basically, do not try to get clever around memory management, because you'll just screw it up.
Also is it a total taboo to do MALLOC_CHECK_=0 so runtime malloc errors don't stop my program? |
In all likelihood you'll just make your program much harder to debug. The memory bugs do not go away just because the program doesn't crash. The memory just silently gets progressively more corrupt until the program crashes and it becomes impossible to tell where the actual bad code is.
For example, if in some place you delete an invalid pointer and the program doesn't crash immediately, the program might crash at an arbitrarily distant future time during, say, a perfectly correct new, or even a call to std::map::operator[]() (as those may allocate memory).
Finally, what things other than dynamic memory do I have to explicitly free up (delete or delete[])? |
None. A pointer must be deleted if and only if it has been allocated by new. The same goes for delete[] and new[].
But, really, you should avoid managing memory manually, if possible. Just use local objects or smart pointers.
Code like this:
1 2 3
|
T *p = new T;
//...
delete p;
|
must be avoided at all costs in exception-safe code.