C order of evaluation of the assignment operator

I met in the case where the cross-platform code behaved differently in the base assignment task.

One compiler first evaluated the Lvalue value, the second Rvalue value, and then the assignment.

Another compiler used Rvalue first, the second Lvalue, and then assignment.

This can make a difference if the Lvalue affects the Rvalue, as shown in the following case:

struct MM {
    int m;
}
int helper (struct MM** ppmm ) { 
    (*ppmm) = (struct MM *) malloc (sizeof (struct MM)); 
    (*ppmm)->m = 1000;
    return 100;
}

int main() { 
    struct MM mm = {500};
    struct MM* pmm = &mm
    pmm->m = helper(&pmm);
    printf(" %d %d " , mm.m , pmm->m);
}

The example above, the line pmm->m = helper(&mm);, depends on the evaluation order. if Lvalue is first evaluated than pmm-> m, it is mm.m, and if Rvalue, calculated first than pmm-> m, is equivalent to the MM instance that is allocated on the heap.

, C ( ), , . , ?

+4
2

= :

. .

(C2011, 6.5.16/3, )

. , , , , . , , , , .

, . , , (C2011, 5.1.2.3/2). :

, undefined.

(C2011, 6.5/2)

, main() pmm, pmm, , undefined.

Undefined . undefined, , ( , ). C , . , , , . - . , , undefined, .

+4

: = . .

, :

*Get() = logf(2.0f);

, , .

. . ; . , , undefined.

- , , .

  • .

, pmm struct mm. , . pmm, , - ;. 1000 m, - ;. 100 , , 100, mm, m.

mm->m 100 ppm->m 1000. , .

  1. .

, , ppm, , . 1000 m, . . , ppm->m , m 100.

mm->m 500, , pmm->m 100. . .

+2

All Articles