标签: compiler-bug

在发布模式下,代码行为不符合预期

以下代码在调试模式和发布模式下生成不同的结果(使用Visual Studio 2008):

int _tmain(int argc, _TCHAR* argv[])
{

    for( int i = 0; i < 17; i++ ) 
    { 
        int result = i * 16;

        if( result > 255 )
        {
            result = 255;
        }

        printf("i:%2d, result = %3d\n", i, result) ; 
    } 

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

调试模式的输出,如预期的那样:

i: 0, result =   0
i: 1, result =  16
(...)
i:14, result = 224
i:15, result = 240
i:16, result = 255
Run Code Online (Sandbox Code Playgroud)

释放模式的输出,其中i:15结果不正确:

i: 0, result =   0
i: 1, result …
Run Code Online (Sandbox Code Playgroud)

c c++ optimization visual-studio-2008 compiler-bug

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

从函数返回结构时可能存在 GCC 错误

我相信我在实现 O'Neill 的 PCG PRNG 时在 GCC 中发现了一个错误。(Godbolt 编译器资源管理器上的初始代码

相乘后oldstate通过MULTIPLIER,(存储在RDI结果),GCC不该结果添加到INCREMENT,movabs'ingINCREMENT到RDX代替,然后把它用作rand32_ret.state的返回值

最小可重现示例(编译器资源管理器):

#include <stdint.h>

struct retstruct {
    uint32_t a;
    uint64_t b;
};

struct retstruct fn(uint64_t input)
{
    struct retstruct ret;

    ret.a = 0;
    ret.b = input * 11111111111 + 111111111111;

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

生成的程序集(GCC 9.2、x86_64、-O3):

fn:
  movabs rdx, 11111111111     # multiplier constant (doesn't fit in imm32)
  xor eax, eax                # ret.a = 0
  imul rdi, rdx
  movabs rdx, 111111111111 …
Run Code Online (Sandbox Code Playgroud)

c assembly gcc x86-64 compiler-bug

131
推荐指数
3
解决办法
6198
查看次数

(这= = null)在C#中!

由于在C#4中修复了一个错误,将打印以下程序true.(在LINQPad中试试)

void Main() { new Derived(); }

class Base {
    public Base(Func<string> valueMaker) { Console.WriteLine(valueMaker()); }
}
class Derived : Base {
    string CheckNull() { return "Am I null? " + (this == null); }
    public Derived() : base(() => CheckNull()) { }
}
Run Code Online (Sandbox Code Playgroud)

在VS2008的发布模式下,它会抛出InvalidProgramException.(在调试模式下,它工作正常)

在VS2010 Beta 2中,它没有编译(我没有尝试Beta 1); 我学到了很难的方法

this == null在纯C#中还有其他方法吗?

c# visual-studio-2010-beta-2 compiler-bug

129
推荐指数
4
解决办法
1万
查看次数

为什么这个Haskell代码用-O运行得慢?

这段Haskell代码运行慢得多-O,但-O应该是非危险的.谁能告诉我发生了什么?如果重要,它是尝试解决这个问题,它使用二进制搜索和持久段树:

import Control.Monad
import Data.Array

data Node =
      Leaf   Int           -- value
    | Branch Int Node Node -- sum, left child, right child
type NodeArray = Array Int Node

-- create an empty node with range [l, r)
create :: Int -> Int -> Node
create l r
    | l + 1 == r = Leaf 0
    | otherwise  = Branch 0 (create l m) (create m r)
    where m …
Run Code Online (Sandbox Code Playgroud)

optimization haskell ghc compiler-bug

86
推荐指数
1
解决办法
2841
查看次数

为什么java 5+中的volatile不能确保来自另一个线程的可见性?

根据:

http://www.ibm.com/developerworks/library/j-jtp03304/

在新的内存模型下,当线程A写入易失性变量V,并且线程B从V读取时,在写入V时对A可见的任何变量值现在都保证对B可见.

互联网上的许多地方声明以下代码永远不应该打印"错误":

public class Test {
    volatile static private int a;
    static private int b;

    public static void main(String [] args) throws Exception {
        for (int i = 0; i < 100; i++) {
            new Thread() {

                @Override
                public void run() {
                    int tt = b; // makes the jvm cache the value of b

                    while (a==0) {

                    }

                    if (b == 0) {
                        System.out.println("error");
                    }
                }

            }.start();
        }

        b = 1;
        a = 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

b …

java concurrency multithreading volatile compiler-bug

69
推荐指数
2
解决办法
4954
查看次数

从int中提升/可空转换的严重错误,允许从十进制转换

我认为这个问题会让我在Stack Overflow上立刻成名.

假设您有以下类型:

// represents a decimal number with at most two decimal places after the period
struct NumberFixedPoint2
{
    decimal number;

    // an integer has no fractional part; can convert to this type
    public static implicit operator NumberFixedPoint2(int integer)
    {
        return new NumberFixedPoint2 { number = integer };
    }

    // this type is a decimal number; can convert to System.Decimal
    public static implicit operator decimal(NumberFixedPoint2 nfp2)
    {
        return nfp2.number;
    }

    /* will add more nice members later */
} …
Run Code Online (Sandbox Code Playgroud)

.net c# nullable compiler-bug implicit-conversion

59
推荐指数
2
解决办法
2857
查看次数

使用printf的%s说明符打印NULL的行为是什么?

遇到一个有趣的采访问题:

test 1:
printf("test %s\n", NULL);
printf("test %s\n", NULL);

prints:
test (null)
test (null)

test 2:
printf("%s\n", NULL);
printf("%s\n", NULL);
prints
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)

虽然这可能在某些系统上运行良好,但至少我的方法正在抛出一个分段错误.这种行为最好的解释是什么?以上代码在C中.

以下是我的gcc信息:

deep@deep:~$ gcc --version
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Run Code Online (Sandbox Code Playgroud)

c linux language-lawyer compiler-bug

48
推荐指数
4
解决办法
4万
查看次数

C++预处理器中R和L有什么特别之处?

我通过Visual Studio 2013预处理器运行以下代码.输出令我惊讶.

hello.cpp的内容:

#define A(j) #j

A(A?)
A(B?)
A(C?)
A(D?)
A(E?)
A(F?)
A(G?)
A(H?)
A(I?)
A(J?)
A(K?)
A(L?)
A(M?)
A(N?)
A(O?)
A(P?)
A(Q?)
A(R?)
A(S?)
A(T?)
A(U?)
A(V?)
A(W?)
A(X?)
A(Y?)
A(Z?)
Run Code Online (Sandbox Code Playgroud)

命令:

cl /P hello.cpp
Run Code Online (Sandbox Code Playgroud)

hello.i包含:

#line 1 "hello.cpp"



"A?"
"B?"
"C?"
"D?"
"E?"
"F?"
"G?"
"H?"
"I?"
"J?"
"K?"
"L"
"M?"
"N?"
"O?"
"P?"
"Q?"
"R"
"S?"
"T?"
"U?"
"V?"
"W?"
"X?"
"Y?"
"Z?"
Run Code Online (Sandbox Code Playgroud)

我试着打电话给A(L?p:q)时碰到了这个,这导致了"Lp:q",这对我不利.

这是正确的,定义明确的C++吗?C++中的L和R有什么特别之处?如果文件具有.c扩展名,则L和R被视为与字母表的其余部分相同.这与C++ 11有关吗?它必须是一个新功能,因为旧版本的MSVS不会以特殊方式使用L和R.

我能做些什么来阻止MSVS 2013以这种特殊方式治疗L和R?

c++ visual-c++ compiler-bug c-preprocessor c++11

47
推荐指数
1
解决办法
1888
查看次数

当C++将元素从函数的返回值存储到std :: vector时出现意外的结果

当函数涉及重新分配时,我发现一些编译器可能在函数调用之前保存地址.它导致存储在无效地址中的返回值.

在上面的描述中有一个例子来解释行为.

#include <stdio.h>
#include <vector> 
using namespace std;

vector<int> A; 
int func() { 
    A.push_back(3);
    A.push_back(4);
    return 5; 
} 
int main() { 
    A.reserve(2);
    A.push_back(0);
    A.push_back(1);
    A[1] = func();
    printf("%d\n", A[1]);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

有一些常见的C++编译器,测试结果如下.

  • GCC(GNU编译器集合):运行时错误或输出 1
  • Clang:输出 5
  • VC++:输出 5

是不确定的行为?

c++ gcc vector compiler-optimization compiler-bug

45
推荐指数
1
解决办法
1866
查看次数

'委托'System.Action'不带0个参数.' 这是一个C#编译器错误(lambdas +两个项目)吗?

请考虑以下代码.看起来完全有效的C#代码对吗?

//Project B
using System;
public delegate void ActionSurrogate(Action addEvent);
//public delegate void ActionSurrogate2();
// Using ActionSurrogate2 instead of System.Action results in the same error
// Using a dummy parameter (Action<double, int>) results in the same error

// Project A
public static class Class1 {
    public static void ThisWontCompile() {
        ActionSurrogate b = (a) =>
                            {
                                a(); // Error given here
                            };
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到一个编译错误'委托'动作'不接受0参数.使用(Microsoft)C#4.0编译器在指定位置.请注意,您必须在另一个项目中声明ActionSurrogate才能显示此错误.

它变得更有趣:

// Project A, File 1
public static class Class1 {
    public static void ThisWontCompile() …
Run Code Online (Sandbox Code Playgroud)

c# lambda compiler-errors compiler-bug

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