Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


delete does not work

delete does not work

delete does not work

Hi, I tried the code in BCB 6 console app:

class Testclass
    double val;

#pragma argsused
int main(int argc, char* argv[])
  Testclass * t1 = new Testclass;
  t1->val = 0;
  int s1 = sizeof(*t1);
  delete t1;
  int s2 = sizeof(*t1);
  t1->val = 10;

There is no AV when trying to adjust t1->val = 10 !
Why is this?
Also, s1 and s2 both equal 8 bytes.  why hasn't the memory been freed?
Also, although it's not in the contract, delete doesn't set t1 to NULL after deletion. Surely it should?

uncommenting //~Testclass(){}; makes no difference

RE: delete does not work

Welcome to the wonderful world of garbage collection. The following quote is from Bruce Eckel's Thinking in C++, 2nd ed. Volume 1


There's another issue, however, and that's the lifetime of an object. If you create an object on the stack or in static storage, the compiler determines how long the object lasts and can automatically destroy it. However, if you create it on the heap, the compiler has no knowledge of its lifetime. In C++, the programmer must determine programmatically when to destroy the object, and then perform the destruction using the delete keyword. As an alternative, the environment can provide a feature called a garbage collector that automatically discovers when an object is no longer in use and destroys it. Of course, writing programs using a garbage collector is much more convenient, but it requires that all applications must be able to tolerate the existence of the garbage collector and the overhead for garbage collection. This does not meet the design requirements of the C++ language and so it was not included, although third-party garbage collectors exist for C++.
So, no, delete does not NULL anything, you have to do it yourself. When the object is cleared from memory is ?????

IMHO, Mr. Eckel's 2 volume books are a great addition to anyone's library. You can find an electronic version of the books on his web site.

James P. Cottingham
I'm number 1,229!
I'm number 1,229!

RE: delete does not work

In C++, when you use delete on a pointer variable, it calls the variable's destructor and then marks the memory used by the pointed to variable as available for allocation.  It will not null out the pointer, you must do it your self if you choose to. Tha is why the following code sequence is commonly used:

   delete P;
   P = 0;

So you do not get an av because the value in t1 still has the value of a pointer to where T1 used to be.  When you execute t1->val = 10, you are changing the value of the memory location that used to be occupied by t1->val, which may have been already reallocated to some other object.  

Long and short of this posting, if you want your pointers to be set to null after deletion, you have to do it yourself. And it is strongly encouraged that you do so if you are not going to re-use the pointer immediately after the deletion to prevent such potential time bombs as above.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close