As a general rule of thumb, to see this you are doing something (reading/writing/freeing) memory that is not yours.
If you're running under Linux, run your code again, but with the command 'valgrind' on the front, to run it under the excellent valgrind memchecker, which will often identify where you're going wrong (you may have to install valgrind, but it's available in many common repositories).
I have installed the valgrind and ran the program with it but it shows many many lines which are all above my head. I have been searching for google about a tutorial and have found one. I will read it soon.