floating point exception

M

Marc Schellens

I would liketo handle a floating point exception in
my program.
As I learned I can do a longjump from the signal handler, but:
1. Will local objects be cleaned up properly?
2. Lets say I divideone array by another array using a for loop:
If I longjump lets say just before the loop, can I access the
loop index variable (if properly declared before) for example?
Are there any caveeats?

thanks,
marc
 
V

Victor Bazarov

Marc Schellens said:
I would liketo handle a floating point exception in
my program.
As I learned I can do a longjump from the signal handler, but:
1. Will local objects be cleaned up properly?
No.

2. Lets say I divideone array by another array using a for loop:
If I longjump lets say just before the loop, can I access the
loop index variable (if properly declared before) for example?

Not sure what you mean.
Are there any caveeats?

Yes. Plenty. That's why exception handling was introduced into
C++. Find out what exception is thrown when an invalid FP operation
is attempted and catch it. Unfortunately, just like with many other
FP-related things, it's not defined by the Standard. Consult your
compiler documentation.

Victor
 
M

Marc Schellens

Victor said:
Not sure what you mean.

Something like:

jmp_buf env;


vector<double> res,src1,src2;

; initialize them

size_type i=0;

if( setjmp( env))
{
res = NAN;
}

for(; i<src.size(); ++i)
{
res = src1/src2;
}


somwhere else:

void FltHandler(int)
{
longjump( env, 1);
}

Yes. Plenty. That's why exception handling was introduced into
C++. Find out what exception is thrown when an invalid FP operation
is attempted and catch it. Unfortunately, just like with many other
FP-related things, it's not defined by the Standard. Consult your
compiler documentation.

Thanks,
marc
 
V

Victor Bazarov

Marc Schellens said:
Victor said:
Not sure what you mean.

Something like:

jmp_buf env;


vector<double> res,src1,src2;

; initialize them

size_type i=0;

if( setjmp( env))
{
res = NAN;


Yes, 'i' is valid here, since 'env' "stores" it, and non-zero
value returned from 'setjmp' means the environment was restored
to the state it was stored in. AFAICT, 'i' will be 0, and if
you won't return from your function right here, you will run
into infinite loop.

Since in C++ it is not recommended to use setjmp and longjmp,
but istead use exceptions, you better ask in comp.lang.c about
those functions.
}

for(; i<src.size(); ++i)
{
res = src1/src2;
}


somwhere else:

void FltHandler(int)
{
longjump( env, 1);
}

Yes. Plenty. That's why exception handling was introduced into
C++. Find out what exception is thrown when an invalid FP operation
is attempted and catch it. Unfortunately, just like with many other
FP-related things, it's not defined by the Standard. Consult your
compiler documentation.

Thanks,
marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top