0

The code below compiles and runs perfectly fine. However, if I replace the line

int d=xgcd(x,y,&m,&n);
cout<<d << " = "<<m<<" * "<<x<<" + "<<n<<" * "<<y<<endl;

with

    cout<< xgcd(x,y,&m,&n) << " = "<<m<<" * "<<x<<" + "<<n<<" * "<<y<<endl;

Then, m and n will be zero rather than what they are supposed to be. So I am wondering what is the reason behind this discrepancy of behavior. Am I missing something?

Here is the code written in C++ and I am using gcc 4.72 with flag -O0.


testXGCD.cpp:

#include"xgcd.h"
#include<iostream>
#include<cstdlib>
using namespace std;
int main(int argc, char** argv) {
    // Read the command line parameter
    int x=atoi(argv[1]), y=atoi(argv[2]);
    int m=0,n=0;
    int d=xgcd(x,y,&m,&n);
    // Output the result;
    cout<<d << " = "<<m<<" * "<<x<<" + "<<n<<" * "<<y<<endl;
}

xgcd.h:

#ifndef XGCD
#define XGCD
#include<vector>
    // Perform the extended Euclidean Algorithm.
    // Find (m,n) and return it. pp and qp are two pointers to store the
    // coefficients of m and n so 1 = pm + qn.
    const int xgcd(int m, int n, int* pp = 0, int* qp = 0) {
        // The temp variable to store the remainder.
        int temp;
        // Create a container to store the quotients.
        std::vector<int> quotients;
        // Start the Euclidean algorithm.
        while(!(m==0)) {
        // store the quotient for the extended Euclidean algorithm.
            quotients.push_back(n/m);
            temp = m;
            m = n%m;
            n = temp;
        }
        // Before performing the magic pattern Bud Brown showed us in class, 
        // put base value 0 and 1 to the end.
        quotients.back() = 1;
        quotients.push_back(0);
        // Create an iterator to traverse the quotients and do the trick.
        typename std::vector<int>::reverse_iterator rip = quotients.rbegin();
        rip+=2;
        // Do the trick here!
        while(rip!=quotients.rend()) {
            *rip=*rip * *(rip-1) + *(rip-2);
            rip++;
        }
        // If there are odd number of quotients, the last value should be
        // it's negative. Otherwise, the second last value should be negative.
        if(quotients.size() % 2) {
            if(pp!=0)
            *pp=-quotients[0];
            if(qp!=0)
            *qp=quotients[1];
        }else{
            if(pp!=0)
            *pp=quotients[0];
            if(qp!=0)
            *qp=-quotients[1];
        }
        return temp;
    }
    #endif

I used a header file rather than c file for xgcd.h since I modified my original template implementation and don't bother to create another separate implementation file for my homework problem.

Does anyone know why is there such a difference?

tgeng
  • 1,768
  • 2
  • 15
  • 20
  • http://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c – chris Feb 11 '13 at 03:30
  • But isn't this a series of function calls? I tried to put call to `xgcd()` at the end of the line and it works correctly. But I am wondering how is the line `cout<< ...<< endl` evaluated from the end? – tgeng Feb 11 '13 at 19:29
  • It takes the form of `function1(function2(function3(), x), y, z);`. You have no guarantees on what order anything is evaluated in. – chris Feb 11 '13 at 19:33

0 Answers0