Home > Archive > VC Language > May 2006 > How does "delete" work?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How does "delete" work?
|
|
|
| Hi All,
How does "delete" work?
Does that equal to
i_Boki = null;
?
Best regards,
Boki.
| |
| Marcus Heege 2006-05-30, 4:14 am |
| Hi Boki
"Boki" <bokiteam@ms21.hinet.net> wrote in message
news:1148982536.196077.138310@j33g2000cwa.googlegroups.com...
> Hi All,
> How does "delete" work?
>
> Does that equal to
>
> i_Boki = null;
>
> ?
>
> Best regards,
> Boki.
delete is a language construct that ensures that a) the destuctor of an
object is called and b) the memory of an object is made available for other
memory allocations.
In theory, you can implement the memory allocation/deallocation logic
manually by overloading operators new and delete, but in most cases, you
will use the default implementation provided by the CRT.
Marcus
| |
| Ulrich Eckhardt 2006-05-30, 8:08 am |
| Boki wrote:
> How does "delete" work?
Delete first invokes the object's destructor and then releases the
associated storage.
> Does that equal to
>
> i_Boki = null;
>
> ?
Does what exactly equal to this?
In case you want to learn C++, this is not the right place, just a support.
Pick a good book from the reviews section on accu.org.
Uli
| |
| Alex Blekhman 2006-05-30, 8:08 am |
| Boki wrote:
> Hi All,
> How does "delete" work?
>
> Does that equal to
>
> i_Boki = null;
No, operator delete does not affect pointer value at all. It
aims at object(s) that pointer points to. Operator delete is
symmetric to operator new:
X* px = new X; // C++ statement
This is what compiler does behind the scenes:
X* px = malloc(sizeof(X)); // allocate enough memory for X
X::X(px); // call X constructor to create object instance
Deleting with operator delete:
delete px; // C++ statement
This is what compiler does behind the scenes:
if(px)
{
X::~X(px); // call X destructor to destroy object
instance
free(px); // free memory
}
HTH
Alex
| |
| Arman Sahakyan 2006-05-30, 8:08 am |
| Hi,
"Boki" wrote:
> Hi All,
> How does "delete" work?
>
> Does that equal to
>
> i_Boki = null;
>
Never!
delete is applicable to a pointer which is pointing an object within the heap.
IOW, if an object is created by 'new', it should be destroyed by 'delete'.
Afterwards, you may assign NULL to the deleted object's pointer (which is
encouraged to do).
--
======
Arman
|
|
|
|
|