开发者

Function calls order, is it a GCC bug? [duplicate]

This question already has an开发者_开发问答swers here: Closed 11 years ago.

Possible Duplicates:

Undefined Behavior and Sequence Points

Order of function call

I have found a problem in the following code when I compile it with GCC 4.5.2 (x86 32 bits).

# include <stdio.h>

int function(int x){
    printf("%d\n", x);
    return 2*x + 1;
}

int main(){
    int x = 3*function(1) + 4*function(2) + 5*function(3) + 6*function(4) + 7*function(5) + 8*function(6);
    printf("%d\n", x);
    return 0;
}

Expected output:

1

2

3

4

5

6

299

Actual GCC output:

1

2

4

3

6

5

299

I compiled the same code with clang and the output is the expected one.


No. The order the calls take place is not specified. They could take place in any order, but for each pair of function calls made here, one will see either all of the side-effects of the other, or none (i.e. they cannot run in parallel unless they have no side effects, and in this example they have a big side effect, printf).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜