Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete does not work

Status
Not open for further replies.

markus3000

Programmer
Aug 24, 2010
9
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
 
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
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top