0

I met a strange issue. The c++ calling convention seems different from win/linux from macosx

The following code's behavior is different on MacOSX/linux/Win7 (64bit)

MacOSX: Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)

Linux: gcc version 4.8.4

Win7: VS2015

On Win7/Linux:

this is method1
this is method2
this is method3
this is method4
this is method5
this is method6
this is method7
this is method8
this is method9

on MacOsx

this is method9
this is method8
this is method7
this is method6
this is method5
this is method4
this is method3
this is method2
this is method1

Here is the code:

#include <iostream>

int   method1(){
    std::cout << "this is method1" << std::endl ;
    return 1;
}

int   method2(){
    std::cout << "this is method2" << std::endl;
    return 1;
}

int   method3(){
    std::cout << "this is method3" << std::endl;
    return 1;
}

int   method4(){
    std::cout << "this is method4" << std::endl;
    return 1;
}

int   method5(){
    std::cout << "this is method5" << std::endl;
    return 1;
}

int   method6(){
    std::cout << "this is method6" << std::endl;
    return 1;
}

int   method7(){
    std::cout << "this is method7" << std::endl;
    return 1;
}

int   method8(){
    std::cout << "this is method8" << std::endl;
    return 1;
}

int   method9(){
    std::cout << "this is method9" << std::endl;
    return 1;
}



void   caller(int, int,int, int,int, int,int, int,int){

}

int main(){
    caller(method1(), method2(),method3(), method4(),method5(), method6(),method7(), method8(),method9());
    return 0;
}
zhoumj
  • 9
  • 3

1 Answers1

1

While the calling conventions in deed differ from platform to platform, this is not the result of different calling conventions, but order of evaluation, which is implementation defined.
This means the order may not only change from OS to OS, but from compiler to compiler (in theory even between different compiler versions). As a result, your code should never rely ona particular evaluation order.

MikeMB
  • 20,029
  • 9
  • 57
  • 102