3

Just wondering since I am working with a partner on a C++ project, is it possible to explicitly delete an object that has been initialized on the stack? (so without pointer)

For example:

MinHeap h(data); // on stack

Vs

MinHeap *h = new MinHeap();

This has never come up before since I always allocated memory on the heap for an large object.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Fox
  • 595
  • 1
  • 4
  • 10
  • The obvious question is why would you want to 'explicitly delete' something on the stack, when it's going cleaned up for you automatically? – john Mar 16 '13 at 20:42
  • Well I actually would not but my partner who is more verced in Java wanted to avoid pointers and initialized the object on the stack. Then this issue came up when we needed to delete it explicitly so I was just wondering about the answer since this has never crossed my mind – Fox Mar 16 '13 at 20:48
  • 1
    Well just tell him that's a fine thing to do, and will cause no problems. You do not need to delete anything. In fact on the stack is the preferred way to do things if you don't need to explicitly control the lifetime of the object. – john Mar 16 '13 at 20:50

3 Answers3

12

A stack variable is always valid in its current scope. You can enforce the scope (and thus freeing the allocated memory) by using curly brackets around the block you want to have the instance valid in:

{
  MinHeap h(data);
  // Do stuff here

} // h gets freed here
Karel Petranek
  • 15,005
  • 4
  • 44
  • 68
3

Short and only answer would be no.

If an object is allocated on the stack it's not actually you that allocates it but the compiler that does it for you. It's also the compilers to responsibility to "unallocate" (or "delete") the object when the scope of the function ends.

The only reason to use delete is to deallocate something you allocated with new.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

is it possible to explicitly delete an object that has been initialized on the stack?

No, it is not possible.

According to Paragraph 5.3.5/2 of the C++11 Standard on delete expressions:

If the operand has a class type, the operand is converted to a pointer type by calling the above-mentioned conversion function, and the converted operand is used in place of the original operand for the remainder of this section. In the first alternative (delete object), the value of the operand of delete may be a null pointer value, a pointer to a non-array object created by a previous new-expression, or a pointer to a subobject (1.8) representing a base class of such an object (Clause 10). If not, the behavior is undefined. [...]

Also relevant is Paragraph 3.7.3/3 about variables with automatic storage duration (i.e. allocated "on the stack"):

If a variable with automatic storage duration has initialization or a destructor with side effects, it shall not be destroyed before the end of its block, nor shall it be eliminated as an optimization even if it appears to be unused, except that a class object or its copy/move may be eliminated as specified in 12.8.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451