LLVM和GCC,不同的输出相同的code [英] LLVM and GCC, different output same code

查看:150
本文介绍了LLVM和GCC,不同的输出相同的code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个示例code只是为了显示从LLVM编译器和GCC的不同输出。我想知道为什么?答案应该是很简单的,但我不能看到它。
(X code 4.6.1)

在code:

 的#include<&stdio.h中GT;的#define MAX(A,B)((a)及GT;(二)(一):(b))的INT增量(){
    静态INT I = 42;
    I + = 5;
    的printf(增量返回%d个\\ N,I);
    返回我;
}INT主(INT ARGC,字符** argv的){
    INT X = 50;
    的printf(%d到%的D最大为%d \\ n,X,增量(),MAX(X,增量()));
    的printf(%d到%的D最大为%d \\ n,X,增量(),MAX(X,增量()));
    返回0;
}

的LLVM输出:

 增量返回47
增量返回52
增量返回57
的50和47 max为57
增量返回62
增量返回67
增量返回72
的50和62 max为72

GCC输出:

 增量返回47
增量返回52
的50和52 max为50
增量返回57
增量返回62
增量返回67
的50和67 max为62


解决方案

参数的评估的顺序并不<击>定义指定。因此,这:

 的printf(%d到%d个最大的为%d \\ n,X,增量(),MAX(X,增量()));

原因 <击>未定义不确定的行为的。这就是为什么你有两种编译器不同的结果。

另外一个(潜在的)问题是: MAX - 这可能会导致两次调用递增。避免使用这样的宏。

This is a sample code just to show a different output from the LLVM compiler and the GCC. I wonder why? The answer should be very simple, but I can't see it. (Xcode 4.6.1)

The code:

#include <stdio.h>

#define MAX(a,b) ( (a) > (b) ? (a) : (b) )

int increment() {
    static int i = 42;
    i += 5;
    printf("increment returns %d\n",i);
    return i;
}

int main( int argc, char ** argv ) {
    int x = 50;
    printf("max of %d and %d is %d\n", x,increment(),MAX(x, increment()));
    printf("max of %d and %d is %d\n", x,increment(),MAX(x, increment()));
    return 0;
}

The LLVM Output:

increment returns 47
increment returns 52
increment returns 57
max of 50 and 47 is 57
increment returns 62
increment returns 67
increment returns 72
max of 50 and 62 is 72

GCC Output:

increment returns 47
increment returns 52
max of 50 and 52 is 50
increment returns 57
increment returns 62
increment returns 67
max of 50 and 67 is 62

解决方案

The order of evaluation of the parameters is not defined specified. So this:

printf("max of %d and %d is %d\n", x,increment(),MAX(x, increment()));

causes undefined unspecified behavior . That's why you have different results on both compilers.

Another (potential) problem is: MAX - it could cause two calls to increment. Avoid using such macros.

这篇关于LLVM和GCC,不同的输出相同的code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆