-
December 1st, 2003, 08:59 AM
#1
Exception when calling delete
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.
Code:
#include<iostream>
#include<stdexcept>
using namespace std;
void main()
{
char* p;
try
{
delete p;
}
catch(exception ex)
{
cout<<"hello"<<endl;
}
}
Regards<br>
Ram
-
December 1st, 2003, 09:02 AM
#2
Re: Exception when calling delete
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.
Code:
#include<iostream>
#include<stdexcept>
using namespace std;
void main()
{
char* p;
try
{
delete p;
}
catch(exception ex)
{
cout<<"hello"<<endl;
}
}
Regards<br>
Ram
You've not allocated anything with new.
try:
Code:
char* p = new char[100];
delete[] p;
Thought for the day/week/month/year:
Windows System Error 4006:
Replication with a nonconfigured partner is not allowed.
-
December 1st, 2003, 09:26 AM
#3
Hi,
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
-
December 1st, 2003, 09:31 AM
#4
Originally posted by Ram_Gupta
Hi,
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.
-
December 1st, 2003, 09:33 AM
#5
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
-
December 1st, 2003, 09:40 AM
#6
Hi,
I just want to know whether i can catch an exception
if call delete on dangling pointer. B'coz in that case my
job would be less tiresome.
Thakx and Regards
Ram.
-
December 1st, 2003, 09:46 AM
#7
Originally posted by Ram_Gupta
Hi,
I just want to know whether i can catch an exception
if call delete on dangling pointer. B'coz in that case my
job would be less tiresome.
Thakx and Regards
Ram.
Yes.
You can catch the exception.
Thought for the day/week/month/year:
Windows System Error 4006:
Replication with a nonconfigured partner is not allowed.
-
December 1st, 2003, 09:53 AM
#8
To reiterate:
It is undefined behaviour
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
-
December 1st, 2003, 10:02 AM
#9
Hi,
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.
-
December 1st, 2003, 10:05 AM
#10
Originally posted by Ram_Gupta
Hi,
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.
-
December 1st, 2003, 10:32 AM
#11
void main()??? That speaks levels...
What complier are you using and what platform are you running under. Regardless if the question/behaviour is undefined as indicated.
-
December 2nd, 2003, 02:03 AM
#12
Hi,
Can u tell me which exception i should catch?
Thankx and Regards
Ram
-
December 2nd, 2003, 04:11 AM
#13
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
-
December 2nd, 2003, 06:02 AM
#14
Originally posted by Ram_Gupta
Hi,
Can u tell me which exception i should catch?
Thankx and Regards
Ram
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.
-
December 2nd, 2003, 08:03 AM
#15
Aside from fully agreeing with the previous posters, you can catch Windows SEH exceptions with
catch(...)
but you don't know what hit you.
All the buzzt
CornedBee
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|