For questions about (suspected) incorrect behavior of a compiler. Use with the appropriate language tag and, where applicable, with the tag for the compiler in question.
Questions tagged [compiler-bug]
309 questions
133
votes
3 answers
Possible GCC bug when returning struct from a function
I believe I found a bug in GCC while implementing O'Neill's PCG PRNG. (Initial code on Godbolt's Compiler Explorer)
After multiplying oldstate by MULTIPLIER, (result stored in rdi), GCC doesn't add that result to INCREMENT, movabs'ing INCREMENT to…

vitorhnn
- 1,043
- 1
- 8
- 7
132
votes
2 answers
In release mode, code behavior is not as expected
The following code generates different results under debug mode and release mode
(using Visual Studio 2008):
int _tmain(int argc, _TCHAR* argv[])
{
for( int i = 0; i < 17; i++ )
{
int result = i * 16;
if( result > 255 )
…

Lorris Lin
- 953
- 2
- 6
- 9
131
votes
6 answers
(this == null) in C#!
Due to a bug that was fixed in C# 4, the following program prints true. (Try it in LINQPad)
void Main() { new Derived(); }
class Base {
public Base(Func valueMaker) { Console.WriteLine(valueMaker()); }
}
class Derived : Base {
…

SLaks
- 868,454
- 176
- 1,908
- 1,964
89
votes
1 answer
Why does this Haskell code run slower with -O?
This piece of Haskell code runs much slower with -O, but -O should be non-dangerous. Can anyone tell me what happened? If it matters, it is an attempt to solve this problem, and it uses binary search and persistent segment tree:
import…

johnchen902
- 9,531
- 1
- 27
- 69
76
votes
4 answers
What is the behavior of printing NULL with printf's %s specifier?
Came across an interesting interview question:
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)
Though this…

Deepanjan Mazumdar
- 1,447
- 3
- 13
- 20
72
votes
7 answers
JDK 11.0.2 compilation fails with javac NPE on anonymous parameterized class type inference
Code (spring-web 5.1.2)
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.AUTHORIZATION, "token");
HttpEntity

Mikhail Kholodkov
- 23,642
- 17
- 61
- 78
69
votes
4 answers
Why doesn't volatile in java 5+ ensure visibility from another thread?
According to:
http://www.ibm.com/developerworks/library/j-jtp03304/
Under the new memory model, when thread A writes to a volatile variable V, and thread B reads from V, any variable values that were visible to A at the time that V was written are…

Oleg
- 6,124
- 2
- 23
- 40
60
votes
2 answers
Serious bugs with lifted/nullable conversions from int, allowing conversion from decimal
I think this question will bring me instant fame here on Stack Overflow.
Suppose you have the following type:
// represents a decimal number with at most two decimal places after the period
struct NumberFixedPoint2
{
decimal number;
// an…

Jeppe Stig Nielsen
- 60,409
- 11
- 110
- 181
47
votes
2 answers
Unexpected result when C++ store element into std::vector from return value of function
When the function involves reallocation, I found some compilers may save the address before the function call. It leads the return value stored in the invalid address.
There is an example to explain behavior in above description.
#include…

Morris Yang
- 482
- 3
- 10
47
votes
2 answers
What's special about R and L in the C++ preprocessor?
I ran the following code through the Visual Studio 2013 preprocessor. The output surprises me.
Contents of hello.cpp:
#define A(j)…

user52875
- 3,020
- 22
- 21
45
votes
3 answers
'Delegate 'System.Action' does not take 0 arguments.' Is this a C# compiler bug (lambdas + two projects)?
Consider the code below. Looks like perfectly valid C# code right?
//Project B
using System;
public delegate void ActionSurrogate(Action addEvent);
//public delegate void ActionSurrogate2();
// Using ActionSurrogate2 instead of System.Action results…

Alex ten Brink
- 899
- 2
- 9
- 19
42
votes
2 answers
Constexpr if with a non-bool condition
I seem to have found something that Clang and GCC disagree on. Here's the code:
int main() {
if constexpr (2) {}
}
This successfully compiles with GCC 7.4.0, but it fails with Clang 7.0.0 with this error message:
test.cpp:3:17: error: constexpr…

Indiana Kernick
- 5,041
- 2
- 20
- 50
39
votes
3 answers
Conditional operator's return type and two-phase lookup
Consider the following snippet:
struct Base { };
struct Derived : Base { };
void f(Base &) { std::cout << "f(Base&)\n"; }
template
void g() {
Derived d;
f(T{} ? d : d); // 1
}
void f(Derived &) { std::cout <<…

Quentin
- 62,093
- 7
- 131
- 191
37
votes
2 answers
'const float' value different than 'float' when casting to 'int' in C#
Can any of you explain why does this happen?
static void Main()
{
const float xScaleStart = 0.5f;
const float xScaleStop = 4.0f;
const float xScaleInterval = 0.1f;
const float xScaleAmplitude = xScaleStop - xScaleStart;
const…

joaojose
- 273
- 3
- 7
35
votes
1 answer
Why do gcc and clang each produce different output for this program? (conversion operator vs constructor)
program:
#include
struct bar_t {
int value;
template
bar_t (const T& t) : value { t } {}
// edit: You can uncomment these if your compiler supports
// guaranteed copy elision (c++17). Either way, it…

verb_noun
- 375
- 5
- 11