At the C++ level, your code is not wrong, it will also free the memory. freeHowever, the runtime of C/C++, that is, libc, will not really free this memory. Because if there is malloc later, he will have to apply for memory from the operating system, so it is better to keep it himself. Currently, all libc will cache the memory by itself to increase the speed of application and release.
PS:
The C++ code you wrote is too bad. If you continue to write it like this in the future, you will suffer a lot
At the C++ level, your code is not wrong, it will also free the memory.
PS:free
However, the runtime of C/C++, that is, libc, will not really free this memory. Because if there is malloc later, he will have to apply for memory from the operating system, so it is better to keep it himself.Currently, all libc will cache the memory by itself to increase the speed of application and release.
The C++ code you wrote is too bad. If you continue to write it like this in the future, you will suffer a lot
What environment does the questioner use? I use MAC + g++ and there is no problem.
In this case, if possible, it is recommended to use smart pointers and STL containers.