0

From my understanding, when you allocate objects on the heap you use

Object* dynamicobject = new Object();

When I call delete I go

delete dynamicobject

I'm confused because I delete the pointer to the instance of that object, but along my line of thinking, you need to actually delete the object in memory itself which requires you to dereference the pointer like delete *dynamicobject but this is incorrect. If you want to change the object a pointer points to, it needs to be dereferenced, and I assumed that the same applied for deletion, but it seems only the pointer can be deleted.

nhgrif
  • 61,578
  • 25
  • 134
  • 173

5 Answers5

3

If you do

delete *dynamicobject;

the value given to the delete operator is the value in the dynamic memory location, not the location itself. The delete operator needs to know where the dynamic memory is, so it can reclaim it.

For example, suppose you do:

int *dynamic_int = new int;
*dynamic_int = 10;

If you then did:

delete *dynamic_int;

the delete operator would receive the integer value 10. That doesn't provide the information it needs to reclaim the dynamic memory where that value is stored. But if you do

delete dynamic_int;

the delete operator receives the address of that dynamic memory, and it can reclaim it.

Barmar
  • 741,623
  • 53
  • 500
  • 612
2

The operators are parallel. new returns a pointer to newly-allocated memory. delete takes a pointer to allocated memory and deletes it. In other words, delete (new ...()) works.

Maybe it would help to consider that delete fundamentally has to work with memory somehow, and not with an object, per se. Thus it needs not only the object but also the object's memory location.

Claudiu
  • 224,032
  • 165
  • 485
  • 680
0

delete dynamicobject; does not delete the pointer dynamicobject. Rather, it deletes the object that dynamicobject points to.

You don't have to (and can't) write delete *dynamicobject;, presumably for similar reasons to why you don't have to write dynamicobject = &new Object;.

user253751
  • 57,427
  • 7
  • 48
  • 90
0

You only deal in pointers, since dynamic memory allocation is just a big linked list of blocks. When you request a block of size n, the allocator searches the linked list for a block that satisfies the request. When the allocator finds the block, it returns a pointer to said block--that is, the address of the first byte in the block.

Freeing memory simply returns the block into the linked list, so that when you try to allocate memory again, that block may be reused.

All the allocator needs to know is the address of the first byte and the size. That's why it deals only in pointers.

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75
0

I think you are thinking about destructing the object. When you call new, memory is allocated for the object and its address is returned as a pointer. That pointer is a unique identifier recording exactly which area of memory needs to be deallocated when the object is deleted.

So delete needs to be given the pointer to the memory to deallocate.

But that doesn't kill the object. Killing the object happens by running the destructor and this is where the pointer is dereferenced.

The destructor function is, of course ~Object() {}.

So delete will dereference the pointer to access the actual memory of the object by running the destructor function. After the destructor has run on the dereferenced object the memory address that the object occupied is released back to the runtime system.

So delete needs the pointer which gets dereferenced to destruct the object itself and is then used to determine exactly what memory to release.

Galik
  • 47,303
  • 4
  • 80
  • 117