everyone. Recently, I have been learning how to develop a function to delete a pointer safely. However, something weird happened when I made my safe_delete()
code shown below.
#include <iostream>
using namespace std;
template <class T>
void safe_delete(T* ptr) {
delete ptr;
ptr = NULL;
}
int main() {
int var = 10;
int* ptr = &var;
safe_delete<int>(ptr);
}
Running the code in repl.lt (I am afraid that it might cause a memory leak), here is what I get:
munmap_chunk(): invalid pointer
exited, aborted
What does the above error mean? I currently guessed that munmap_chunk()
is some function used in the implementation of delete
.
The above source code is taken from stack overflow's answers. Something is weird. I just added templates to the function, which I believe is correct.
Can anyone tell me why is this error happening?