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 can’t possibly be freed has the heap doesn’t work that way.

Variables don’t get deleted. That is meaningless. Memory gets deallocated depending on how it was allocated:

  • on stack -> function return
  • allocated with new -> delete or auto_ptr or other smart pointer (which calls delete)
  • allocated with malloc -> free
  • global variable -> process exit

Browse More Popular Posts

Leave a Comment