I just wrote a code in C++ which does some string manipulation, but when I ran valgrind over, it shows some possible memory leaks. Debugging the code to granular level I wrote a
To avoid the memory leak, return the status from main
instead of calling exit
. If you're returning zero, you can omit the return
statement if you like; the program will exit with a status of zero in that case.
This also raise another question for me, is such a code harmful?
On a modern operating system, it won't cause any harm - all resources are reclaimed automatically when a program terminates. However, it does make it harder to use tools like Valgrind to find genuine problems, so it's best to avoid even harmless memory leaks if you can.