How Does Delete Work

How does delete work?

In general, this is implementation-dependent.

The way this is usually implemented is that new/malloc will allocate slightly more bytes than you asked for, and will use the extra bytes for some book-keeping: many times the allocated blocks will also be maintained in a linked-list, and so the extra bytes will be used to store the next/prev pointer, as well as the size of the block.

This is not mandatory, however. The new/malloc calls can just as well store your pointer and the block's size in, say, a map.

If you're interested in a specific implementation, you will have to provide more information (e.g. what runtime/compiler are you using?).

What does delete command really do for memory, for pointers in C++?

Think of memory as a big warehouse with lots of boxes to put things into. When you call "new", the warehouse staff finds an unused box large enough for your needs, records that box as being owned by you (so it's not given to someone else), and gives you the number of that box so you can put your stuff into it. This number would be the "pointer".

Now, when you "delete" that pointer, the reverse happens: the warehouse staff notes that this particular box is available again. Contrary to real warehouse staff they aren't doing anything with the box — so if you look into it after a "delete" you might see your old stuff. Or you might see somebody else’s stuff, if the box was reassigned in the meantime.

Technically, you are not allowed to look into your box once you have returned it to the pool, but this is a somewhat weird warehouse with no keys or guards, so you can still do whatever you want. However, it might cause problems with the new owner of the box, so it's expected that you follow the rules.

How does delete[] know the size of the operand array?

When you allocate memory on the heap, your allocator will keep track of how much memory you have allocated. This is usually stored in a "head" segment just before the memory that you get allocated. That way when it's time to free the memory, the de-allocator knows exactly how much memory to free.

How does delete operator in javascript works?

The delete operator just deletes all properties from an array element. Because looking up the element takes O(1) and deleting the properties takes O(1) the whole thing takes O(1). Be careful tho, delete does not change the length property of the array nor does it change the indexes of other elements within the array. So the behaviour is as follows:

const arr = [0,1,2,3,4,5]
delete arr[2]
console.log(arr[2]) // undefined

Why does delete[] work in one case but not another?

My thought was that I assign arr = arrtemp so that pointer arr now points to the added array, and I delete[] arrtemp should have no effect on arr,

Strictly speaking, this much is true. However, if you take this viewpoint, then strictly speaking, delete[] arrtemp should have no effect on arrtemp. Deleting the memory to which arrtemp points does not change arrtemp; it still points to the same memory location. What has changed is that accessing that memory is no longer valid.

but it does

Strictly speaking, this is false. Deleting the memory to which arrtemp points has no effect on arr. And that likely accounts for the symptoms you are seeing. There is no effect on arr, so arr still points to the same memory as before the deletion, to the same memory to which arrtemp points, to the memory that is no longer valid to access because it has been deleted.

Before the deletion:

-----------
| arr |---\
----------- |
| --------------------
same address |--->| allocated memory | valid to access
| --------------------
----------- |
| arrtemp |---/
-----------

After the deletion:

-----------
| arr |---\
----------- |
| ~~~~~~~~~~~~~~~~~~~~
same address |--->( deleted memory ) invalid to access
| ~~~~~~~~~~~~~~~~~~~~
----------- |
| arrtemp |---/
-----------

There is no change to arr (the pointer), but there is a change to *arr (the pointed-to object). This distinction is important. When arr == arrtemp, then deleting *arrtemp also deletes *arr. The situation is not merely that *arr is equivalent to *arrtemp, but that *arr is *arrtemp. Two names for the same object. Calling delete[] arrtemp directs the computer to delete the array (starting at) *arrtemp, a.k.a. *arr in your situation. Accessing that array after the deletion is invalid, regardless of which name you use to access the array.

How does delete operator work with pointers in c?

Allocators typically hide allocation information just before the pointer in question. The allocation includes that space, but the pointer is moved after it so you don't access/modify it. This is part of why writing to a pointer at a negative index breaks things so badly.

As noted in the comments, your code is broken as written, since you used delete ptr;, not delete[] ptr;; only the latter knows to look for the information needed to destruct the whole array, not just a single element.

How does the delete in C++ know how many memory locations to delete

There is only delete operator, free exist only as function. Under C++, you are encouraged to use new/delete over malloc()/free().


There is an internal "magic" in delete operator. When an array is created with new[], the size of array is stored in the metadata at the memory block. delete[] makes use of that information.

All this is of course compiler-, OS-, optimizer- and implementation-dependent.



Related Topics



Leave a reply



Submit