valgrind shows definitely lost blocks in simple code

How can i remove memory leaks in simple code below?
1
2
3
4
5
6
int main() {
    char* str = new char();
    str = "test";
    //delete str;
    return 0;
}

1
2
g++ main.cpp
valgrind --leak-check=yes  ./a.out

1
2
3
4
...
==14600== LEAK SUMMARY:
==14600==    definitely lost: 1 bytes in 1 blocks
...

Thanks,
The leak occurs at line 3, where the only copy of the pointer to the dynamically-allocated char is destroyed by assignment. Fixing this depends on what you want the program to do. Currently it has no observable behavior at all.
Topic archived. No new replies allowed.