简体   繁体   English

为什么即使使用-O0标志,​​clang也会优化我的数组?

[英]Why is clang optimizing out my array even when using -O0 flag?

I am trying to debug the following C Program using GDB: 我正在尝试使用GDB调试以下C程序:

// Program to generate a user specified number of 
// fibonacci numbers using variable length arrays
// Chapter 7  Program 8   2013-07-14

#include <stdio.h>

int main(void)
{
    int i, numFibs;
    printf("How many fibonacci numbers do you want (between 1 and 75)?\n");
    scanf("%i", &numFibs);

    if (numFibs < 1 || numFibs > 75)
    {
        printf("Between 1 and 75 remember?\n");
        return 1;
    }

    unsigned long long int fibonacci[numFibs];

    fibonacci[0] = 0;  // by definition
    fibonacci[1] = 1;  // by definition

    for(i = 2; i < numFibs; i++)
        fibonacci[i] = fibonacci[i-2] + fibonacci[i-1];

    for(i = 0; i < numFibs; i++)
        printf("%llu ", fibonacci[i]);

    printf("\n");

    return 0;
}

The issue I am having is when trying to compile the code using: clang -ggdb3 -O0 -Wall -Werror 7_8_FibonacciVarLengthArrays.c 我遇到的问题是在尝试使用以下代码编译代码时: clang -ggdb3 -O0 -Wall -Werror 7_8_FibonacciVarLengthArrays.c

When I try to run gdb on the a.out file created and I am stepping through the program execution. 当我尝试在创建的a.out文件上运行gdb时,我正在逐步执行程序。 Anytime after the fibonacci[] array is decalared and I type: info locals the result says fibonacci <value optimized out> (until after the first iteration of my for loop) which then results in fibonacci holding the address 0xbffff128 for the rest of the program (but dereferencing that address does not appear to contain any meaningful data). 在fibonacci []数组被decalared之后的任何时候我输入:info locals结果说fibonacci <value optimized out> (直到我的for循环的第一次迭代之后),然后导致fibonacci为程序的其余部分保存地址0xbffff128 (但取消引用该地址似乎不包含任何有意义的数据)。

I am just confused why clang appears to be optimizing out this array when the -O0 flag is used? 我很困惑为什么当使用-O0标志时clang似乎正在优化这个数组?

I can use gcc to compile this code and the value displays as expected when using GDB.... Any thoughts? 我可以使用gcc编译这段代码,使用GDB时,值会按预期显示....有什么想法?

Thank you. 谢谢。

You don't mention which version of clang you are using. 你没有提到你正在使用哪个版本的clang。 I tried it with both 3.2 and a recent SVN install (3.4). 我在3.2版本和最近的SVN安装版本(3.4)中都尝试过。

The code generated by the two versions looks pretty similar to me, but the debugging information is different. 这两个版本生成的代码看起来与我非常相似,但是调试信息不​​同。 The clang 3.2 (which comes from a default ubuntu 13.04 install) produces an error when I try to examine fibonacci in gdb: 当我尝试检查gdb中的斐波那契时,clang 3.2(来自默认的ubuntu 13.04安装)产生错误:

fibonacci = <error reading variable fibonacci (DWARF-2 expression error: DW_OP_reg operations must be used either alone or in conjunction with DW_OP_piece or DW_OP_bit_piece.)>

In the code compiled with clang 3.4, it all works fine. 在使用clang 3.4编译的代码中,一切正常。 In neither case is the array "optimized out"; 两种情况都不是“优化”数组; it's clearly allocated on the stack. 它清楚地分配在堆栈上。

So I suspect the oddity that you're seeing has more to do with the emission of debugging information than with the actual code. 因此,我怀疑您所看到的奇怪之处更多在于调试信息的发布而不是实际代码。

gdb does not yet support debugging stack allocated variable-length arrays. gdb尚不支持调试堆栈分配的可变长度数组。 See https://sourceware.org/gdb/wiki/VariableLengthArray 请参阅https://sourceware.org/gdb/wiki/VariableLengthArray

Use a compile time constant or malloc to allocate fibonacci so that it will be visible to gdb. 使用编译时间常数或malloc分配fibonacci,以便gdb可以看到它。

See also GDB reports "no symbol in current context" upon array initialization 另请参见阵列初始化时GDB报告“当前上下文中没有符号”

clang is not "optimizing out" the array at all! clang根本没有“优化”阵列! The array is declared as a variable-length array on the stack, so it has to be explicitly allocated (using techniques similar to those used by alloca() ) when its declaration is reached. 该数组在堆栈上声明为可变长度数组,因此必须在达到其声明时显式分配(使用类似于alloca()使用的技术)。 The starting address of the array is unknown until that process is complete. 在该过程完成之前,数组的起始地址是未知的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么clang对于使用-O1而不是-O0编译的c代码会产生错误的结果? - Why does clang produces wrong results for my c code compiled with -O1 but not with -O0? 为什么gcc -o0比icc -o0快? - Why would gcc -o0 be faster than icc -o0? gcc -O0仍然优化了“未使用”的代码。是否有一个编译标志来改变它? - gcc -O0 still optimizes out “unused” code. Is there a compile flag to change that? 为什么 clang 使用 -O0 产生低效的 asm(对于这个简单的浮点和)? - Why does clang produce inefficient asm with -O0 (for this simple floating point sum)? 为什么 gcc -O3 处理 avx256 compare intrinsic 的方式不同于 gcc -O0 和 clang? - Why does gcc -O3 handle avx256 compare intrinsic differently than gcc -O0 and clang? "GCC - 没有关于带有 -O0 的未初始化数组的警告" - GCC - no warning about an uninitialized array with -O0 为什么gcc使用-O0进行一些优化 - Why gcc does some optimizations with -O0 GDB 仍然显示变量<optimized out>即使在 gcc (v7 & v9.3.1) -O0 优化关闭之后</optimized> - GDB still showing variables <optimized out> even after gcc (v7 & v9.3.1) -O0 optimization turned off Apple:使用 -O0 与 -O2(内核)编译 clang 帧大小 - Apple: Compile clang frame size with -O0 vs -O2 (kernel) 为什么将GCC设置为O0时仍会进行优化? - Why GCC still optimized something while set to O0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM