3

I am trying to learn the best habits and practices in C++, particularly surrounding memory management. I have been spoiled on this front by using ARC in my iOS apps, and the built-in GC in Java, as well as a few other languages where GC is enabled.

I understand that you use delete or delete[] to deconstruct pointers. My question is, how do you delete integers, or other variables of a base data type?

My first thought was that since delete seems to only work with pointers, can I do this:

int intToDelete = 6;
delete &intToDelete;

So basically, can you create a pointer to an integer in memory, and delete the integer using that pointer?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 8
    You don't delete `intToDelete`. When it goes out of scope, it ceases to exist. Automatically. – Daniel Fischer Dec 07 '12 at 02:14
  • 3
    I was going to answer, but then realized I would write something very similar to [this](http://stackoverflow.com/questions/8839943/why-does-the-use-of-new-cause-memory-leaks/8840302#8840302). – R. Martinho Fernandes Dec 07 '12 at 02:15
  • @R.MartinhoFernandes, I see what you did there. – chris Dec 07 '12 at 02:15
  • Thanks for the REALLY quick answers! I actually just got a book over C++, and everything comes pretty naturally with memory management being the only exception! Thanks again, and sorry for my "beginner's ignorance." –  Dec 07 '12 at 02:17
  • 2
    Your profile says you know Objective-C. Note that this is doing the exact same thing as it does in Objective-C and C - it's just automatic storage on the stack. – Pubby Dec 07 '12 at 02:25
  • @Pubby Thanks for relating it to something I understand better. I get now that it all depends on the scope of the variable, and unless you allocate something in global memory, it ceases to exist once its scope is no longer relevant. Thanks! –  Dec 07 '12 at 02:36

3 Answers3

12

delete and delete[] should only be used with pointers which you allocated explicitly with new or new[], respectively. In particular, for every time you use new, you should have a corresponding delete. Similarly, each new[] needs a corresponding delete[]. You should never use either of these with variables for which you do not explicitly allocate memory. The compiler takes care of memory allocation (and deallocation) for all non-pointer variables.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
1

delete is for releasing the memory in heap allocated by new operator, and delete[] is the counterpart for new[]. You cannot use delete to release a pointer which was not allocated by new, even a pointer from malloc.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Healer
  • 290
  • 1
  • 7
  • 1
    You CAN use delete[] for malloc()-allocated memory in most implementations of C++ because it's just overridden free() for basic types. But it's better not to rely on this. – Pavel Ognev Dec 07 '12 at 06:26
0

Local objects clean themselves up automatically. This is one of the most useful features of the language, as it can also be extended through RAII to cover non-local objects. Specifically, the use of delete is a massive red flag that you did not use the correct automatic cleanup tools, and delete[] is even worse.

Correct use of construction and destruction semantics is essential to well-written C++ code. If you're manually managing memory, you're doing it wrong, unless you have a doddering old compiler or some very unique circumstances (which I have never, ever seen any examples of that actually justify this).

Puppy
  • 144,682
  • 38
  • 256
  • 465