Pointers and Initialization¶
Be careful!
Be careful whenever creating a new raw pointer
e.g. like this
This here will allocate to the memory.See:
- https://godbolt.org/z/WqnT8957Y
-
https://godbolt.org/z/7xPTYxWMx - To showcase raw pointers within structs and de-constructors
- https://godbolt.org/z/vjvn3aj49
Such pointers need to be manually deleted, or else it will result in a Memory Leak.
To delete you'd do this here below. Just make sure it hasn't been deleted already, otherwise it's gonna give a "double free error".
delete p_IntTest;
// Optionally, you can set it to nullptr
// https://stackoverflow.com/a/49509254/11161500
p_IntTest = nullptr;
However, in C++ there are very additional special pointers, e.g. std::unique_ptr
Note on godbolt -fsanitize=address
can hint on Memory Leaks. LeakSanitizer doesn't exist on Windows though.
But there are workarounds for it as well.
Additionally:
-Wall -Wextra -Werror -Wpedantic
or on Windows /W4
, I think.