By Madiyar, 5 years ago, ,

My another blog hopefully will help to prevent from bugs.

Try to guess the output without using compiler or IDE. Please hide your answers to not spoil.

1.

#include <iostream>
using namespace std;

int main() {
int a = 020;
short b = 2;
cout << a - b << endl;
return 0;
}


2.

#include <iostream>
using namespace std;

int main() {
bool ok = true;
// Try to guess with if condition  \
if (!ok && true)
cout << "I am ok" << endl;
return 0;
}


3.

#include <iostream>
using namespace std;

int main() {
unsigned a = 0;
int b = 2;
if (a + b >= -2)
cout << a + b << ">=" << -2 << endl;
else
cout << a + b << "<" << -2 << endl;
return 0;
}


4.

#include <iostream>
using namespace std;

int main() {
int  a = 0, b = 1;
if (a&b==0) {
cout << (a&b) << "=" << 0 << endl;
} else {
cout << (a&b) << "!=" << 0 << endl;
}
return 0;
}


• +96

 » 5 years ago, # |   -34 5. #include using namespace std; int main() { int a = 17, b = -2; cоut << a - - b << endl; return 0; } 
•  » » 5 years ago, # ^ | ← Rev. 2 →   +24 Right answer in ed.1
 » 5 years ago, # |   +15
•  » » 5 years ago, # ^ | ← Rev. 3 →   0 Edit : Got it. Thanks
•  » » » 5 years ago, # ^ | ← Rev. 3 →   0 spoiler
•  » » » 5 years ago, # ^ | ← Rev. 2 →   +2 Spoiler
•  » » 5 years ago, # ^ |   +2 Good job.All of them right!
•  » » » 5 years ago, # ^ |   +1 Finding bugs is easier if you know there is one :P
 » 5 years ago, # | ← Rev. 2 →   -8 Spoiler
 » 5 years ago, # | ← Rev. 2 →   +13 Spoiler
 » 5 years ago, # | ← Rev. 2 →   +16 is it easy question? then just choose the unexpected answer :P
 » 5 years ago, # |   +59 Here you can see my set of examples. What do you think about them?
•  » » 5 years ago, # ^ | ← Rev. 2 →   +5 Cool :) Most of them make sense actually, except #16. Can you explain, please, why it's happening?
•  » » » 5 years ago, # ^ | ← Rev. 4 →   +10 The snippet number 16 invokes undefined behavior. So a question why is not applicable here.
•  » » » 5 years ago, # ^ | ← Rev. 2 →   0 OH-MY-GOD. Now I'm trying to remember contests in which I wrote such code... It's such an incredibly insidious bug... It's a very unlucky coincidence of three ingredients: assignment operator evaluates v[0] before f() (I'm not sure if it HAS to be done in this order, but it may), vector has its own capacity (initially it's 1). If you add more elements than it can hold, it allocates more memory somewhere else (and it's usually twice as much memory as before), this way we set the value f() returned to the memory where v[0] was before executing f(). It triggers the undefined behaviour (and in this case "nothing" happens). For example you can notice that if you have 1 or 2 or 4 elements initially in vector, the whole thing gets screwed up, but having 3 or 5 elements "works" perfectly (there was no need to reallocate the vector). IT'S INSANE...
•  » » » » 5 years ago, # ^ |   0 Yes, problem is exactly in this.
•  » » 5 years ago, # ^ | ← Rev. 2 →   0 interesting :))another version for problem 2: #include using namespace std; int main(){ int n; cin >> n; // input 60 int a = 1 << n; cout << a << endl; return 0; } I'm wondering why their behaviors are different!I'd like to know the explanation for problem 1, what does 2[a][b] refer to?
•  » » » 5 years ago, # ^ | ← Rev. 6 →   +3 2[a][b] is a shorten of *(*(2+a)+b) and equivalent to b[a[2]].You can always assume x[y] is same as *(x+y) and y[x]. But it's not suggested to code like 10[a] because only some programmers coding in assembly language will like that!Correct: in C++ some x[y] is operator [] of typeof(x). They're not equivalent with *(x+y) or y[x]. For example, if x is a vector.
•  » » » 5 years ago, # ^ |   0 Anyway, behavior is undefined in this case. I think, if disable optimizations, they will both output 228. With enabled optimizations, compiler probably tries to inline this constant and make it in different way.
•  » » 4 years ago, # ^ |   +6 18 is impossibly evil
 » 5 years ago, # | ← Rev. 4 →   -11 deleted
•  » » 5 years ago, # ^ | ← Rev. 2 →   +6 spoiler
 » 5 years ago, # | ← Rev. 2 →   0 Spoiler.
•  » » 5 years ago, # ^ | ← Rev. 2 →   0 Misread b = 2 in #3 as b = -2. Never do this...
 » 5 years ago, # |   +13 One more: #include using namespace std; int main() { int a = 228, b = 322; cout << (a == b) ? 228 : 322; } 
•  » » 5 years ago, # ^ |   +17 Nice example (had to compile to understand) but g++ a.cpp 2>&1| pbcopy a.cpp:6:21: warning: operator '?:' has lower precedence than '<<'; '<<' will be evaluated first [-Wparentheses] cout << (a == b) ? 228 : 322; ~~~~~~~~~~~~~~~~ ^ a.cpp:6:21: note: place parentheses around the '<<' expression to silence this warning cout << (a == b) ? 228 : 322; ^ ( ) a.cpp:6:21: note: place parentheses around the '?:' expression to evaluate it first cout << (a == b) ? 228 : 322; ^ ( ) a.cpp:6:23: warning: expression result unused [-Wunused-value] cout << (a == b) ? 228 : 322; ^~~ 2 warnings generated. 
 » 5 years ago, # |   +21 Another one #include using namespace std; int main() { long long a = 1e18 + 1; cout << a << endl; return 0; } 
•  » » 5 years ago, # ^ |   +38 Why everybody downvoting? Try to compile and run. Output is : 10^18 not 10^18+1.Is this because of the double representation?
•  » » 5 years ago, # ^ |   0 It is due to the IEEE 754 floating point shortcomings. Here is a very nice explanation
•  » » 5 years ago, # ^ |   0 Funny example. I'll add it to collection?
•  » » » 5 years ago, # ^ |   0 Of course, yes. And, please, fix 11 example. Probably it has encoding problems. My browser shows ??/ instead of backslashes.
•  » » » » 5 years ago, # ^ |   +8 Well, that's not encoding problem. It is exactly what i want to write.
•  » » 4 years ago, # ^ |   -50 I'd like to add one then! #include #include using namespace std; int main() { int a = pow(5,4); cout << a << endl; return 0; } 
 » 4 years ago, # |   -13 What is the output of the following program? #include int main() { int a = 0; int b = 0; // Iterate 3 times for (size_t i = 1; i <= 3 || a * b < 10; i++) { int mult = 9000 * i; a += mult; b += mult; if (a == b) { std::cout << "Good" << std::endl; } else { std::cout << "Bad" << std::endl; break; } } } 
•  » » 4 years ago, # ^ |   0 A very nice question. :) Excellent use of how the overflow works.
 » 4 years ago, # |   -39 Nice bugs!Here is one more #include using namespace std; void swap(int a,int b) { int t; t=a; a=b; b=t; } int main() { int x=5,y=2; swap(x,y); cout<
•  » » 4 years ago, # ^ |   -34 Above bug can be prevented by using own C++ swap function.This is one of my most hated bugs. #include using namespace std; int main() { int x=12; if(x/5==2.4) cout<<"Number is 12"; else cout<<"Number is NOT 12"; } 
•  » » » 4 years ago, # ^ | ← Rev. 2 →   +2 It's not bug. And I think it will be more weird if (int) / (int) = (double).
•  » » » » 4 years ago, # ^ |   -13 No, it's a bug... But it's user-made bug, not compiler's one (it's about your link)It's most frequently made by n00bs, and those, who use duck-typing languages, like Python, in their day-to-day work — http://ideone.com/R903zc
•  » » » » » 4 years ago, # ^ |   0 This will only work in Python3, right? If i remember correctly, Python2 treats division like C++, if you divide 12 by 5 you will get 2 unless you import special division module
•  » » » » » » 4 years ago, # ^ |   0 Right :)
•  » » » » » » 4 years ago, # ^ |   0 There is // operator for integer division. Click.
•  » » » 4 years ago, # ^ |   0 Above bug can be prevented by using own C++ swap function. This is not a bug that function doesn't change outside variables, when you pass them by value. Use references: void swap(int &a, int &b) 
 » 4 years ago, # |   0 After giving all of them a try and referring this:I still don't get the first one. Why is 020 considered in octal? I didn't get the 3rd one either. Any help?I did the other 2 correct. Second one was easy (and smart too). For fourth, I suppose that most people assume brackets while reading (They read it something like this : if ((a&b)==0) : although they are careful while writing) and hence must have processed the if part, instead of else! Am I right?
•  » » 4 years ago, # ^ |   0 You can use numbers in base 8 (octal) by writing their octal representation preceded by a zero. So, 020 is equal to 20(octal)=16(dec), 02731 is equal to 2731(octal)=1497(dec), etc.
•  » » » 4 years ago, # ^ |   0 Thanks for the help. I get it now. But I'm still not sure where would this trick be useful? Any help regarding that?
•  » » » » 4 years ago, # ^ |   0 You're welcome :)I have only used this trick in simple base conversion problems. Didn't actually need it in algorithmic problems until today.
•  » » » » 4 years ago, # ^ |   0 If you use bitwise operations (this is a rare thing, but still is useful in some real-life problems like coding a chess engine and sometimes in competitive programming), it is more convenient to work with power2-based systems, because you need to know binary representation just looking at number. For example, it is easy to see that hexadecimal 123 (0x123 in C++, Python and most other languages) is the same as binary 0001 0010 0011 and also equals octal 443 (100 100 011). It is not so obvious, however, that decimal 291 is the same.And the binary system itself can be inconvenient to work with directly because of length of numbers.
 » 3 years ago, # |   -26 hi please i have a runtime error on test 1 with this code any help??? package graph.party; import java.util.ArrayList; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Party { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("entrez le nombre des employés\n"); int n = in.nextInt(); ArrayList tab=new ArrayList(n); int i=0; while(i mySet = new HashSet(tab); tab.clear(); tab=new ArrayList(mySet); System.out.println(tab.size()); } } 
•  » » 3 years ago, # ^ |   0 hi, ok, no.
 » 3 years ago, # | ← Rev. 4 →   +11 In 2008, I learned C language by solving these tricky puzzles:http://www.gowrikumar.com/c/index.phpAfter so many years, I still don't see a better collection of C language puzzles.Example from the page:The following is a piece of C code, whose intention was to print a minus sign 20 times. But you can notice that, it doesn't work.  #include int main() { int i; int n = 20; for( i = 0; i < n; i-- ) printf("-"); return 0; } Well fixing the above code is straight-forward. To make the problem interesting, you have to fix the above code, by changing exactly one character. There are three known solutions. See if you can get all those three.
•  » » 3 years ago, # ^ | ← Rev. 2 →   0 Spoiler!
•  » » 3 years ago, # ^ | ← Rev. 2 →   0 Spoiler
•  » » 3 years ago, # ^ | ← Rev. 3 →   0 Spoilerone way is to change i < n to i + nUPDATE — second way is to change i < n to -i < n
•  » » » 3 years ago, # ^ |   0 Please, hide your solution like X-tazy and rais.fathin38 did.
•  » » » » 3 years ago, # ^ |   0 done :)