Yes, a problem with pointers. Most likely you are using one that is not initialized properly, but it is also possible that you messed up memory management with double releases or some of those.
To avoid uninitialized pointers as local variables, try declaring them as late as possible, preferably (and this is not always possible) when they can be initialized with a significant value. Convince yourself that they will be valuable before they are used by learning the code. If you have difficulty with this, initialize them with a null pointer constant (usually write as NULL or 0 ) and check them.
To avoid uninitialized pointers as element values, make sure they are properly initialized in the constructor and correctly handled in copy constructors and assignment operators. Do not rely on the init function to manage memory, although you can for a different initialization.
If your class does not need copy constructors or assignment operators, you can declare them as private member functions and never define them. This will cause a compiler error if they are used explicitly or implicitly.
Use smart pointers, if applicable. The big advantage here is that if you stick to them and use them sequentially, you can completely avoid the delete entry and nothing will be deleted twice.
Use C ++ strings and container classes whenever possible, instead of C-style strings and arrays. Consider using .at(i) rather than [i] , because this will force a border check. See if you can install your compiler or library to check boundaries on [i] , at least in debug mode. Segmentation errors can be caused by buffer overflows that write garbage over perfectly good pointers.
Performing these steps will significantly reduce the likelihood of segmentation failures and other memory problems. They will certainly not be able to fix everything, and why you should use valgrind from time to time when you have no problems, and valgrind and gdb when you do this.
David Thornley Sep 15 '10 at 17:28 2010-09-15 17:28
source share