Hi,
When i am trying to run the code below,it gives runtime<br>
exception. Although i am ctching the exception.<br>
Plz suggest something. I cann't check the pointer against<br>
NULL.
Originally posted by Ram_Gupta
Hi,
When i am trying to run the code below,it gives runtime<br>
exception. Although i am ctching the exception.<br>
Plz suggest something. I cann't check the pointer against<br>
NULL.
I agree with u. Actually it was a test code. In original one
class is having some pointer data member. In the destructor
he is freeing the memory but doesn't assign the pointer to
NULL. So somehow he has assigned the object to someone
else, so that pointer has become a dangling one. I don't
want to change his overloaded = oparetor nor do i wanna
provide a copy ctor. So i wanna call delete, catch the exception
and cooly come out without any runtime error.
I agree with u. Actually it was a test code. In original one
class is having some pointer data member. In the destructor
he is freeing the memory but doesn't assign the pointer to
NULL. So somehow he has assigned the object to someone
else, so that pointer has become a dangling one. I don't
want to change his overloaded = oparetor nor do i wanna
provide a copy ctor. So i wanna call delete, catch the exception
and cooly come out without any runtime error.
Thanx and Regards
Ram
Why do you want to call delete on something that you know is a "dangling pointer"?
Thought for the day/week/month/year: Windows System Error 4006:
Replication with a nonconfigured partner is not allowed.
Just to recap: the example code given by the OP invokes undefined behaviour, since the delete is called on a pointer that has not been allocated by new and it is not NULL.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there. -- Gordon Bell
This means that, just because this particular compiler (or version of the compiler) throws an exception, you can't rely on that. The next upgrade to your compiler may do something different. For example, it may just call exit() immediately. It might reboot your machine or reformat the hard disk: it could do anything: that's what undefined behaviour means.
Fix the problem, don't try to patch it over. Your desire not to provide a copy ctor is unreasonable. Do a deep copy, not a shallow one; implement a reference counted smart pointer if you don't want a deep copy, but do something positive to address the problem, rather than rely on behaviour that is intrinsically unreliable.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there. -- Gordon Bell
I am trying my best to trace the actual problem. Out of curosity
i just wanna experiment that. But the code above is still
throwing runtime error dialog box...that means it's not going
into the catch block.
I am trying my best to trace the actual problem. Out of curosity
i just wanna experiment that. But the code above is still
throwing runtime error dialog box...that means it's not going
into the catch block.
Thanx and Regards
Ram.
Right. You're catching the wrong type of exception. I didn't indicate which "type" of exception to catch, because of the reasons Graham stated. You asked if something is possible. Yes, it is possible. Don't rely on it. Fix the problem "the right way" - Graham offered several suggestions. Abandon the path you seem to be headed down.
Thought for the day/week/month/year: Windows System Error 4006:
Replication with a nonconfigured partner is not allowed.
You shouldn't be catching any exceptions, you should be fixing the underlying problem!
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there. -- Gordon Bell
As others have indicated and I'll say so too, the behaviour is undefined, but under WinDoze it would be a SException presumably of type access violation, if one occurred, and that's a crap shoot on where in your prog you might encounter one, since your freeing memory that may be in use somewhere else in your program.
There is a couple of underlying issues.
1. whatever complier your using should catch this, and issue a warning about using an uinit'd var.
2. The debug build of your particular memory management routines should catch this also.
3. it's undefined because it all depends I can't imagine 1 and 2 not occuring
so your code should never be getting into the field with this type of error.
Bookmarks