Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

markus3000 (Programmer) (OP)
26 Aug 10 9:07
Hi, I tried the code in BCB 6 console app:

class Testclass
{
  public:
    double val;
    //~Testclass(){};
};

#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;
  cout<<t1->val;
}

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
 
2ffat (Programmer)
26 Aug 10 12:47
Welcome to the wonderful world of garbage collection. The following quote is from Bruce Eckel's Thinking in C++, 2nd ed. Volume 1

Quote:

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!

Prattaratt (TechnicalUser)
26 Aug 10 14:54
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.

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