#PG?!

Hello.
Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int f(int  p, int q, int n)
{
    int     m;
    if( p%q >= n)
       return n;
    m = n * f( p/2, q/2, n/2 );
    return ( m*q );
}
int main()
{
    cout << f(65, 15, 15);
    
    cin.get();
    return  0;
}


Please run it on different platforms and tell me the result.
Last edited on
I'm not sure what you are trying to do, but line 3 will never execute with your inputs, and you'll be stuck in an infinite loop when q and n become zero.
why is it that line 8 will never execute?

btw, I get a general page fault on my computer, an intelĀ® processor, and would like to know if it generates any different result on any other PC.
Last edited on
Line 8 will be executed but not line 9 because p%q >= n is never true.
I get a general page fault on my computer, an intelĀ® processor, and would like to know if it generates any different result on any other PC.

This program performs integer division by zero (line 8, 5th iteration of the loop), and, that's what I observe on Linux/intel, Solaris/sparc, and AIX/power7.

The actual outputs:

$ ./test.linux.tsk
Floating point exception (core dumped)
$ ./test.sundev1.tsk 
Arithmetic Exception
$ ./test.ibm.tsk
Trace/BPT trap (core dumped)


Bonus: valgrind on Linux says

==37064== Process terminating with default action of signal 8 (SIGFPE): dumping core
==37064==  Integer divide by zero at address 0x521DD8F
==37064==    at 0x804873C: f(int, int, int) (test.cc:8)
==37064==    by 0x8048772: f(int, int, int) (test.cc:10)
==37064==    by 0x8048772: f(int, int, int) (test.cc:10)
==37064==    by 0x8048772: f(int, int, int) (test.cc:10)
==37064==    by 0x8048772: f(int, int, int) (test.cc:10)
==37064==    by 0x80487B2: main (test.cc:15)
Last edited on
Topic archived. No new replies allowed.