Memory leak C++

后端 未结 10 539
野趣味
野趣味 2021-02-02 06:02

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

10条回答
  •  无人及你
    2021-02-02 06:42

    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.

提交回复
热议问题