-6

If you set a pointer to null, the variable is deleted. It can't be used anymore and it won't take your storage anymore. And the name for the pointer represents its value. Its value is always &someVariable. So could I do this: &someVariable = null;

[EDIT] I was very new to C++ when I wrote this, so I had multiple misconceptions.

To delete a pointer, you must delete ptr; instead of setting it to null via ptr = nullptr;.

&someVariable is an rvalue, so it can't get anything assigned.

It's 0, NULL or nullptr, not null.

In summary, you should never delete &anything;, even if you're lucky and it compiles. Just delete the pointer you got when allocating anything.

Timon Paßlick
  • 193
  • 1
  • 11
  • 1
    Did you try? It shouldn't take more than 5 minutes to whip up some sample code – UnholySheep May 16 '17 at 16:38
  • 7
    Also: *"If you set a pointer to null, the variable is deleted"* - no, nothing is deleted in C++ if you set a pointer to null – UnholySheep May 16 '17 at 16:40
  • I asked google but forgot to try. And how do you delete variables then? SoloLearn told me you had to set the pointer to null. That app must be written by a drunken teddy bear. – Timon Paßlick May 16 '17 at 16:46
  • _"And how do you delete variables then?"_. In reality you want to use smart pointers and RAII and don't delete anything manually, but technically answer is: _by using `delete`_ – Revolver_Ocelot May 16 '17 at 16:47
  • You don't delete variables - variables are a compile time construct (and there would be no point to deleting them either). Setting a pointer to null only makes that pointer point to null, nothing else. The only thing you `delete` in C++ is dynamically allocated memory (allocated using `new`) – UnholySheep May 16 '17 at 16:47
  • Thank you. Why wouldn't there be a point to deleting them? It's less memory. – Timon Paßlick May 16 '17 at 16:50
  • you aren't deleting a variable, you are deleting dynamically allocated memory. – AresCaelum May 16 '17 at 16:51
  • Ok, so does this work? – Timon Paßlick May 16 '17 at 16:51
  • `int x = new int[5]; delete x;` – Timon Paßlick May 16 '17 at 16:52
  • For that you would want to use `delete [] x;` if you call new with [] you delete with [], if you call new without brackets you delete without brackets. – AresCaelum May 16 '17 at 16:52
  • 2
    I don't know what SoloLearn is but don't try to learn C++ from a random app on your mobile phone. Get [a proper, peer-reviewed book written by someone who actually knows what they're talking about](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Lightness Races in Orbit May 16 '17 at 16:53
  • Ok. I'm learning on another tutorial than SoloLearn, but I want to start my project. Hope I'll make less drastic fales when I progress. – Timon Paßlick May 16 '17 at 16:55
  • 1
    No, not a tutorial. A book. A proper, paper book. – Lightness Races in Orbit May 16 '17 at 16:55
  • learncplusplus.com seems to be good. they don't tell you you can learn it fast and have many examples. – Timon Paßlick May 16 '17 at 16:55
  • Did you mean `Java`? – Galik May 16 '17 at 16:56
  • Why Java? What do you mean? Java was my first language, yes... – Timon Paßlick May 16 '17 at 16:57
  • The behavior you describe sounds like `Java`. – Galik May 16 '17 at 17:07
  • Sorry, wrong spelling of the website: It's called `learncpp.com`. – Timon Paßlick May 16 '17 at 17:07
  • Java was my first programming language, so I try to figure out how a new language works with what I've already learned about it and `Java`. – Timon Paßlick May 16 '17 at 17:09
  • No offence but you are not qualified to determine whether it is good or not :) (Although at first glance that one looks a lot better than most) – Lightness Races in Orbit May 16 '17 at 17:13
  • I'm not able to determine if it explains the C++ questions well, but I'm able to rate the style as I'm not such a noob in Java. But you're right, I can't determine if it's good. That's why I wrote that it seems to be good. I knew I couldn't determine that. – Timon Paßlick May 16 '17 at 17:16
  • Let's see who's drunk.  I also use **SoloLearn** to learn C++ and their courses are pretty good. I don't know where you read that but what their course says is `"for local variables on the stack, managing memory is carried out automatically. On the heap, it's necessary to **manually** handle the dynamically allocated memory, and use the **delete** operator to free up the memory when it's no longer needed".` – Tonik May 18 '17 at 13:09
  • That's pretty embarassing. I shouldn't write so late at night. Apologies to the SoloLearn team. – Timon Paßlick May 18 '17 at 14:23
  • I usually check my sources and write in a neutral and friendly tone, I'm sorry. – Timon Paßlick May 18 '17 at 14:26

2 Answers2

7

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
Joshua
  • 40,822
  • 8
  • 72
  • 132
2

If you set a pointer to null, the variable is deleted.

No, it isn't.

It can't be used anymore and it won't take your storage anymore.

That is not true.

So could I do this: &someVariable = null;

No. Even if you wrote nullptr instead of the non-existent null, and even if you could modify an rvalue of built-in type, it would only set the value of some temporary pointer to nullptr.

Modifying a pointer has absolutely no effect on the thing it used to point to.

It sounds like you have become confused regarding delete and setting pointers to null.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055