Advertisements

Vasileios Zografos <> wrote in message news:<bdpsaj$77o$>...
> 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:

See <http://www.dinkumware.com/htm_cpl/signal.html> for info on
signals.

That said, segfaults come from programming errors. Fix them; don't
ignore them.
> 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)?

Vasileios Zografos <> wrote in message news:<bdqesn$dbc$>...
> > As hard as I try, I really can't believe you... sorry ;-))
> >
>
> I said "trust" not "believe" .
> 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.

Vasileios Zografos wrote:
> 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.
> 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

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

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!