C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: segmentation fault exception handling

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Ivan Vecerina
Guest





PostPosted: Sun Jun 29, 2003 10:56 pm    Post subject: Re: segmentation fault exception handling Reply with quote



"Vasileios Zografos" <vzografos (AT) bcs (DOT) org.uk> wrote

Quote:
Hi, I have the following piece of code which sometimes throws a
segmentation fault, because this.x, this.y, this.z might be unallocated
(and it is not possible to initialize them). Its more of a geometric
problem than programming fault.

Seg-faults during access to data members usually indicates that
the whole object itself has not been allocated adequately.

Does the 'this' pointer happen to be NULL when the segfault occurs?
Maybe what you want to do is a check such as:
if( this==0 ) .... // handle error condition...

It works in practice on some platforms, but is a bad idea in
any case, as this formally leads to undefined behavior.

As others mentioned, it *is* a programming fault to call
an object that hasn't been allocated and constructed.
The design of your code needs to be changed to avoid
such situations.

Feel free to provide more details if you want further guidance.

hth,
--
Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
Soft Dev Manger, XiTact <> http://www.xitact.com
Brainbench MVP for C++ <> http://www.brainbench.com



Back to top
Vasileios Zografos
Guest





PostPosted: Mon Jun 30, 2003 5:34 pm    Post subject: Re: segmentation fault exception handling Reply with quote



So, in standard c++ there is no way you can catch a seg. fault?

is there any good way of checking if pointer/variable is initialized or
not? (apart from using if (this==0) that someone suggested is not very
good)?


V.Z.

P.S.
The reason why the variables sometime stay unitialised comes from a
geometrical problem and is nothing much I can do about it. Trust me on
this ;)

Back to top
Vasileios Zografos
Guest





PostPosted: Mon Jun 30, 2003 10:51 pm    Post subject: Re: segmentation fault exception handling Reply with quote



Quote:
As hard as I try, I really can't believe you... sorry Wink)


I said "trust" not "believe" Very Happy.
You cant get people to believe in anything these days




Back to top
Shane Beasley
Guest





PostPosted: Tue Jul 01, 2003 12:50 am    Post subject: Re: segmentation fault exception handling Reply with quote

Vasileios Zografos <vzografos (AT) bcs (DOT) org.uk> wrote


Quote:
So, in standard c++ there is no way you can catch a seg. fault?

ISO C (and thus also C++) allows you to catch and handle the SIGSEGV
signal (SEGV is short for "segmentation violation"). However, the only
sane reason to do so is to save work before aborting the program; the
behavior of a program which ignores SIGSEGV (i.e., one which continues
running after SIGSEGV) is undefined. For example:

#include <iostream>
#include <csignal>
#include <cstdlib>

void say_hi (int) {
// prevent infinite recursion if say_hi() causes another segfault
std::signal(SIGSEGV, SIG_DFL);

std::cout << "hi" << std::endl;
std::abort();
}

int main () { std::signal(SIGSEGV, say_hi); int *p = 0; *p = 0; }

See signals.

That said, segfaults come from programming errors. Fix them; don't
ignore them.

Quote:
is there any good way of checking if pointer/variable is initialized or
not? (apart from using if (this==0) that someone suggested is not very
good)?

Make sure it's always initialized; then you don't have to check.

- Shane

Back to top
Bob Bell
Guest





PostPosted: Tue Jul 01, 2003 1:33 am    Post subject: Re: segmentation fault exception handling Reply with quote

Vasileios Zografos <vzografos (AT) bcs (DOT) org.uk> wrote

Quote:
As hard as I try, I really can't believe you... sorry Wink)


I said "trust" not "believe" Very Happy.
You cant get people to believe in anything these days

Even so, this "sometimes it's uninitialized" thing is the source of
your problem. C++ goes to great lengths to guarantee that objects are
constructed properly. Perhaps you can say a little more about why your
problem circumvents these guarantees. At the very least, it would
satisfy the curiosity of the readers of this thread. ;-)

As far at catching segmentation faults, C++ doesn't even recognize
"segmentation fault" in the first place, much less define and
exception for it. The only things standard C++ recognizes as an
exception is something that originates with a throw statement.
Segmentation faults don't do this.

Another way to look at it is that exceptions are meant for conditions
to which your program can meaningfully respond. A segmentation fault
is likely to be something which can't be handled meaningfully, except
in very rare, controlled circumstances, and even then it's really a
matter of luck if it works. Thus, exceptions are really the wrong way
to deal with segmentation faults.

Bob Bell

Back to top
Rolf Magnus
Guest





PostPosted: Tue Jul 01, 2003 11:31 am    Post subject: Re: segmentation fault exception handling Reply with quote

Vasileios Zografos wrote:

Quote:
So, in standard c++ there is no way you can catch a seg. fault?

Right. Again, note that your program is in an indeterminate state after
a segfault. The best thing you might be able to do to react to it would
be to try write unsaved data to disk (it might work or it might not)
and then immediately terminate.
A practical example of what could happen:
If e.g. you write beyond the bounds of an array, some of the OSs (or
standard library's) memory management data could be overwritten,
resulting in a segfault on the next allocation. Then your program can't
allocate or deallocate any memory, and if it tries, it could easily
corrupt other parts of the memory or segfault again.
A segfault is not something that is part of a regular program execution.
It means your program crashed due to an invalid access, and trying to
recover after a crash is not likely to work.

Quote:
is there any good way of checking if pointer/variable is initialized
or not? (apart from using if (this==0) that someone suggested is not
very good)?


V.Z.

P.S.
The reason why the variables sometime stay unitialised comes from a
geometrical problem and is nothing much I can do about it. Trust me on
this Wink

I won't trust you on this. Dereferencing uninitialized pointers is _not_
a geometrical problem, no matter what your program is supposed to do.
It's a logical problem in your program code. Now trust me :-)



Back to top
Pete Becker
Guest





PostPosted: Tue Jul 01, 2003 2:10 pm    Post subject: Re: segmentation fault exception handling Reply with quote

Shane Beasley wrote:
Quote:

Q: Why is <signal.h> in ISO C anyway, then, if it's not portably useful?


Portable code can handle a signal that's raised by a call to raise.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.