Skip to content

Pointers and Initialization

Be careful!

Be careful whenever creating a new raw pointer

e.g. like this

int* p_IntTest = new int(5);
This here will allocate to the memory.

See:


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.