English 中文(简体)
这是否是一种加速优化的 b? [闭门]
原标题:Is this a gcc optimization bug? [closed]
  • 时间:2011-10-28 10:32:52
  •  标签:
  • c++
  • gcc
  • gcc3

我的守则如下:

bool func(const MY_STRUCT *const ptr, some_struct *x, int y)
{
    printf("IN: %p
", ptr); // ok
    for (int i = 0; i < y; ++i) {
        if (ptr->foo(x[i].key) == 0) {
            return false;
        }
    }
    printf("OUT: %p
", ptr); // ok
    return true;
}

void process(void)
{
    ... ...
    for (i = 0; i < num; ++i) {
        MY_STRUCT *ptr = obj->GetPtr(); // success
        printf("BEFORE: %p
", ptr); // ok
        if (func(ptr, x, y) == false) {
            continue;
        }
        printf("AFTER: %p
", ptr); // <nil> when compile with -O2
        printf("%s", ptr->data); // *** segment fault here ***
    }
}

产出:

BEFORE: 0x0612d3fa
IN: 0x0612d3fa
OUT: 0x0612d3fa
AFTER: <nil>
segment fault

If I compile above code with -O0, everything works fine. However, if I compile it with -O2, after the function func is called, the ptr become NULL!

这是否是一种诱骗? 是否有任何人遇到过类似的ug?

缩略语

最佳回答

您在<代码>func上可能会有(类似)的分量,该编码在打字机的打字上超过<代码>ptr。 确保在<代码>func中的任何阵容或结构出入均受约束。 如果是我的头等高温,我可以说,这几乎肯定是实际的。 即便如此,这也是最可能的解释。

Giving you the benefit of the doubt on that point, if your code is really as posted in your question, then yes, this may be a compiler bug:

    printf("BEFORE: %p
", ptr); // ok
    if (func(ptr, x, y)) {
        continue;
    }
    printf("AFTER: %p
", ptr); // <nil> when compile with -O2

<代码>ptr按数值通过,func,因此,无论在<代码>func内发生什么情况,都不应改变打字功能的价值。

然而,你再次使用一个真正古老的编纂者;即使你报告这一丑闻,也不会出现。 造成这种情况的任何因素都很可能在<条码>gcc<>/条码>上作了长期改动。 采用新的版本或不同的汇编者。

问题回答

如果你看到你所公布的法典中所描述的问题,那么你的海合会就会在 任何优化水平上被打破。

根本就不应达到有问题的法典。

。 回归者总是真实的,如果返回真实的话, lo体的最后一部分是ski熟的。

如果你怀疑一个编目器,从撰写一份照相复制的编码样本开始。

当我们不了解你的actual守则所看的情况时,我们不可能猜测海湾合作委员会的行为是否正确。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签