我在这里遗漏了一些简单的东西(运行时执行优先级?)

P M*_*cki 3 c++ pointers side-effects function-calls operator-precedence

执行这个简单的代码:

int  foo(int* a){
    cout <<"a="<<a;
    *a=1;
    cout <<", *a="<<*a<<endl;
    return 0;}

int main () {
    int* ptr;
    ptr=new int[2];
    ptr[0]=0;
    ptr[1]=0;

    cout<< foo(ptr) <<" "<< ptr <<" *ptr="<< *ptr <<endl;
    cout<< foo(ptr) <<" "<< ptr <<" *ptr="<< *ptr <<endl;

    return 0;}
Run Code Online (Sandbox Code Playgroud)

导致(linux):

a=0x939f008, *a=1
0 0x939f008 *ptr=0
a=0x939f008, *a=1
0 0x939f008 *ptr=1
Run Code Online (Sandbox Code Playgroud)

请解释为什么*ptr = 0在第二行,但不在第四行; 可能是,"东西" cout从右到左被"取出" ?比 - 它如何真正起作用(在运行时一步一步)?

Alo*_*ave 5

根据C++标准,函数参数的评估顺序是未指定的.
它可能是:

  • 从左到右或
  • 从右到左或
  • 任何其他订单

一个我以前的答案在这里,在深度和详细说明了这一点.