2

I've got a floating point exception in huge application after some changes. I tried to comment my changes and found that FPE happens when I enable one simple function call.

api::getMaxSize();

which simply returns value. looks like this

int api::getMaxSize() { return 536870912; };

This is static member function. When I move this to header file everything works fine. I'm confused, what can be the reson? Looks like API is in another module and linked as dynamic library, but how can this cause a problem?

added

There is function maxBox() which is template and implemented in api.h header file. This function calls getMaxSize()

template <typename T>
static rectangle<T> maxBox()
{
    return rectangle<T>(
        getMinSize(), getMinSize(),
        getMaxSize(), getMaxSize()
    );
}

here is the calling code

if (!api::maxBox<double>().contains(box * scale)) { /* api::getMaxSize(); */ }

If I enable getMaxSize() call the program starts throwing FPE, but getMaxSize() is actually never called.

added Found FPE in box * scale, can't understand why it was working without getMaxSize() call, but however the problem is solved. Thanks to everybody.

Thanks in advance.

axe
  • 21
  • 3
  • 1
    In what environment are you compiling/running that? – Diego Sevilla May 20 '11 at 08:55
  • That really sounds strange. Got a minimal example? Also, which OS? – BЈовић May 20 '11 at 08:56
  • 1
    @axe: When and where does the crash happen? There is no floating point here anywhere. – Björn Pollex May 20 '11 at 08:56
  • Oh, sorry. gcc, linux x64. I cannot provide a minimal example, this is huge system and I get FPE in reports generated by tests. Running tests hangs the application. I see that there are no floating points and that's my confusion too. I don't get it at all.. – axe May 20 '11 at 08:59
  • 1
    Maybe some other library you're linking has a same-named function which is being called by accident...? Try changing the name temporarily, or checking at the point of call whether you really get the value you expect. – Tony Delroy May 20 '11 at 09:00
  • I don't know where exactly, I can only see that it stops working after I enable that line. Currently debugging to see if there is anything more then that. Currently I'm debugging, I'll post additional information as soon as I get it. – axe May 20 '11 at 09:01
  • @Tony: I'll see it in a minute under debugger. – axe May 20 '11 at 09:01
  • Do you have a condition depending on the result of `getMaxSize`? Could be that the code causing the exception simply doesn't get called when you leave the call to `getMaxSize` in. Also, you should try running in a debugger and have a breakpoint/watchpoint (term seems to vary between different systems/vendors) on the exception, so you can tell where exactly it is happening. (Alternatively place a breakpoint on the exception class' constructor) – Axel May 20 '11 at 09:07
  • debugger doesn't show anything usefull. When I press "next" to see next instruction it gones to some strange location (not a catch block) and hangs stops debugging.. I really don't understand what happens. – axe May 20 '11 at 09:08
  • 1
    @Axel: I had, but currently no. It's just a simple call to getMaxSize(). I'll try to put cout to see what happens. – axe May 20 '11 at 09:09
  • WOW! It's not even called. Condition before this line is called and possibly can cause FPE, but if I leave this condition without `getMaxSize()` everything works fine. Also if I move `getMaxSize` implementation to the header file it works fine as well. – axe May 20 '11 at 09:14
  • @axe Could it be that the compiler optimizes the condition out if you don't do anything in the `if`? – James Kanze May 20 '11 at 09:18
  • added more details in original post. – axe May 20 '11 at 09:20
  • @Space_C0wb0y: SIGFPE has nothing to do with floating point, despite its name. It is usually raised when dividing integers by zero, or when signed arithmetic overflows (unsigned arithmetic must wrap around). – Alexandre C. May 20 '11 at 09:52
  • Also reading [the wikipedia entry on SIGFPE](http://en.wikipedia.org/wiki/SIGFPE) seems to indicate that an overflow in the multiplication can cause the FPE signal to be raised. – Alexandre C. May 20 '11 at 09:53

1 Answers1

1

Floating point exceptions (actually signals) are raised for different reasons. The main ones are:

  • you divide an integer by zero
  • an operation on signed integers overflows (unsigned integers must wrap around silently in C and C++).

As you can see, they have nothing to do with floating point numbers ! The name is historical and cannot be changed without breaking a lot of source code (there is a SIGFPE constant in <signal.h>).

It can be here that GetMaxSize returns a value which is not representable by a int.

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197