Linked Questions
30 questions linked to/from Why does this loop produce "warning: iteration 3u invokes undefined behavior" and output more than 4 lines?
343 votes
21 answers
24k views
Is uninitialized local variable the fastest random number generator?
I know the uninitialized local variable is undefined behaviour(UB), and also the value may have trap representations which may affect further operation, but sometimes I want to use the random number ...
243 votes
14 answers
24k views
Why does this for loop exit on some platforms and not on others?
I have recently started to learn C and I am taking a class with C as the subject. I'm currently playing around with loops and I'm running into some odd behaviour which I don't know how to explain. #...
142 votes
6 answers
18k views
Why does integer overflow on x86 with GCC cause an infinite loop?
The following code goes into an infinite loop on GCC: #include <iostream> using namespace std; int main(){ int i = 0x10000000; int c = 0; do{ c++; i += i; ...
86 votes
4 answers
6k views
Program behaving strangely on online IDEs
I have come across the below C++ program (source): #include <iostream> int main() { for (int i = 0; i < 300; i++) std::cout << i << " " << i * 12345678 << ...
62 votes
8 answers
5k views
Is accessing a global array outside its bound undefined behavior?
I just had an exam in my class today --- reading C code and input, and the required answer was what will appear on the screen if the program actually runs. One of the questions declared a[4][4] as a ...
75 votes
4 answers
6k views
C++ compilation bug?
I have the following code: #include <iostream> #include <complex> using namespace std; int main() { complex<int> delta; complex<int> mc[4] = {0}; for(int di = 0; ...
17 votes
4 answers
3k views
Does Unary + operator do type conversions?
Till now I was believing that there is no use of unary + operator. But then I came across with following example: char ch; short sh; int i; printf("%d %d %d",sizeof(ch),sizeof(sh),sizeof(i))...
25 votes
2 answers
10k views
Is infinite loop still undefined behavior in C++ if it calls shared library?
It's said that infinite loop for(;;); is undefined behavior. From http://en.cppreference.com/w/cpp/language/memory_model In a valid C++ program, every thread eventually does one of the following: ...
10 votes
5 answers
5k views
what's the point using unsigned int in C?
I thought that unsigned int could store only integers >= 0. But I tried assigning a negative to an unsigned int, nothing special happened. It seems like it stored the value with no problem. So what ...
16 votes
2 answers
9k views
How I'm supposed to use the sanitizer in clang?
I'm sorry if this is a uber-easy concept, but I find hard to acquire the right mindset in order to correctly use the sanitizer provided by clang. float foo(float f) { return (f / 0); } I compile this ...
4 votes
6 answers
6k views
Why does GCC -O2 and -O3 optimization break this program?
I've written this C code for finding the sum of all integers which are equal to the sum of the factorial of their digits. It takes a minute or so to get the job done without any GCC optimization flags,...
5 votes
2 answers
1k views
Why code with multiple nested loops can finish immediately on GCC but take forever on VS?
long long r = 0; long long k = 0; for (; k < 9999999999999; k++) { for (long long i = 0; i < 9999999999999; i++) { for (long long j = 0; j < 9999999999999; j++) { ...
13 votes
2 answers
828 views
Division by zero and undefined behaviour in C
In this paper is the following example of a piece of code that can trigger a division-by-zero: if (arg2 == 0) ereport(ERROR, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("...
2 votes
1 answer
3k views
Using a boost thread: Signal and wait for termination
i'm currently writing a c/c++ dll for later use mostly in Delphi and i'm more familiar with threads in Delphi than c/c++ and especially boost. So i wonder how i can achieve the following scenario? ...
4 votes
1 answer
5k views
g++ "warning: iteration ... invokes undefined behavior" for Seemingly Unrelated Variable
Consider the following code in strange.cpp: #include <vector> using namespace std; int i = 0; int *bar() { ++i; return &i; } int main() { for(size_t j = 0; j < ...