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;
}
5.
Right answer in ed.1
My guesses
Edit : Got it. Thanks
spoiler
Spoiler
Good job.All of them right!
Finding bugs is easier if you know there is one :P
Spoiler
Spoiler
is it easy question? then just choose the unexpected answer :P
Here you can see my set of examples. What do you think about them?
Cool :) Most of them make sense actually, except #16. Can you explain, please, why it's happening?
The snippet number 16 invokes undefined behavior. So a question why is not applicable here.
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:
v[0]
beforef()
(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),f()
returned to the memory wherev[0]
was before executingf()
. 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...
Yes, problem is exactly in this.
interesting :))
another version for problem 2:
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?2[a][b]
is a shorten of*(*(2+a)+b)
and equivalent tob[a[2]]
.You can always assume. But it's not suggested to code likex[y]
is same as*(x+y)
andy[x]
10[a]
because only some programmers coding in assembly language will like that!Correct: in C++ some
x[y]
isoperator []
oftypeof(x)
. They're not equivalent with*(x+y)
ory[x]
. For example, ifx
is avector
.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.
18 is impossibly evil
deleted
spoiler
Spoiler.
Misread
b = 2
in #3 asb = -2
. Never do this...One more:
Nice example (had to compile to understand) but
Another one
Why everybody downvoting? Try to compile and run.
Output is : 10^18 not 10^18+1.
Is this because of the double representation?
Funny example. I'll add it to collection?
Of course, yes. And, please, fix 11 example. Probably it has encoding problems. My browser shows ??/ instead of backslashes.
Well, that's not encoding problem. It is exactly what i want to write.
I'd like to add one then!
What is the output of the following program?
Nice bugs!
Here is one more
Above bug can be prevented by using own C++ swap function.
This is one of my most hated bugs.
It's not bug. And I think it will be more weird if (int) / (int) = (double).
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
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
Right :)
There is // operator for integer division. Click.
This is not a bug that function doesn't change outside variables, when you pass them by value. Use references:
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?
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.
Thanks for the help. I get it now. But I'm still not sure where would this trick be useful? Any help regarding that?
You're welcome :)
I have only used this trick in simple base conversion problems. Didn't actually need it in algorithmic problems until today.
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.
hi please i have a runtime error on test 1 with this code any help???
hi, ok, no.
In 2008, I learned C language by solving these tricky puzzles:
http://www.gowrikumar.com/c/index.php
After 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.
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.
Spoiler!
Spoiler
one way is to change i < n to i + n
UPDATE — second way is to change i < n to -i < n
Please, hide your solution like P_Nyagolov and rais.fathin38 did.
done :)