相关疑难解决方法(0)

GCC -Wuninitialized/-Wmaybe -ininitialized问题

我正在经历一个非常奇怪的问题gcc-4.7 (Ubuntu/Linaro 4.7.2-11precise2) 4.7.2.我没有警告就无法编译以下有效代码:

extern void dostuff(void);

int test(int arg1, int arg2)
{
    int ret;

    if (arg1) ret = arg2 ? 1 : 2;

    dostuff();

    if (arg1) return ret;

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

编译选项和输出:

$ gcc-4.7 -o test.o -c -Os test.c -Wall
test.c: In function ‘test’:
test.c:5:6: warning: ‘ret’ may be used uninitialized in this function [-Wmaybe-uninitialized]
Run Code Online (Sandbox Code Playgroud)

但是,以下代码编译时没有警告(虽然组装效率稍低):

extern void dostuff(void);

int test(int arg1, int arg2)
{
    int ret;

    if (arg1 && arg2) ret = 1;
    if (arg1 && …
Run Code Online (Sandbox Code Playgroud)

c c++ gcc gcc-warning compiler-optimization

14
推荐指数
1
解决办法
2万
查看次数

标签 统计

c ×1

c++ ×1

compiler-optimization ×1

gcc ×1

gcc-warning ×1