Is `&variable = null;` possible? [closed]

No you can’t. &someVariable is an rvalue. It cannot be assigned to and it is meaningless to try. Furthermore, the very idea of what you are trying to do is meaningless. Function variables are allocated on the stack and are only freed when the function returns. Global variables can’t be freed. Variables within allocated structures … Read more

Pointer to a given memory address -C++

Yes, you can construct a pointer that refers to some arbitrary address in memory, by initialising the pointer with the address directly, instead of with an expression like &a: int* ptr = (int*)0x1234ABCD; // hex for convenience std::cout << *ptr; Be careful, though, as it is rare that you know such a memory address exactly. … Read more

Dangling Pointers after Destructor is called

After an object is destroyed, it ceases to exist. There is no point in setting its members to particular values when those values will immediately cease to exist. The pattern of setting pointers to NULL when deleteing the objects they point to is actively harmful and has caused errors in the past. Unless there’s a … Read more

Avoid memory leaks in C++ Pointers [closed]

I follow some simple rules: don’t do manual memory allocations. If you find yourself writing a manual delete, stop and think twice. use std::string instead of C-style strings use std::vector<T> instead of C-arrays (or std::array<T> for fixed-sized arrays) use std::unique_ptr and std::make_unique by default use std::shared_ptr if necessary use RAII wrappers for other resources, e.g. … Read more

A short C++ program about the pointer

An array of arrays like yours look like this in memory +———+———+———+———+———+———+ | x[0][0] | x[0][1] | x[0][2] | x[1][0] | x[1][1] | x[1][2] | +———+———+———+———+———+———+ The location of x, x[0] and x[0][0] is all the same. Also, arrays naturally decays to pointers to their first element. If you use plain x you will get … Read more