Re: Cutest bug you have ever seen?
Quote:
Originally Posted by Hobson
I was getting incorrect results although it was guaranted there should be no overflow for unsigned long long type. Still, result was incorrect. Find an error and get a rep point from me :)
I know! :D
this is pretty simple to me, but I am not a pro ;).
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "Testing";
//iterate through string backwards
for(size_t pos = s.length() - 1; ; --pos) {
cout << s[pos] << endl;;
if(pos == 0) break;
}
}
Code:
#include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
int main()
{
vector<unsigned long long> vec;
vec.push_back(123456789012LL);
vec.push_back(987654321098LL);
unsigned long long result = accumulate(vec.begin(), vec.end(), 0LL);
cout << result;
}
Do you want an explanation too? :D
Re: Cutest bug you have ever seen?
Good job! And no need for explanation, I got it by myself, however, when you are learning, it may take you some time :)
Re: Cutest bug you have ever seen?
I guess the accumulate issue was some time ago Hobson? This is captured by the compiler today.
Re: Cutest bug you have ever seen?
well the compiler gives a warning now
Re: Cutest bug you have ever seen?
I got stumped on this one for a while when I first learnt C. :rolleyes:
Code:
// Oops, infinite loop.
unsigned char c;
for (c = 0; c < 256; c++)
{
/// Do stuff.
}
Re: Cutest bug you have ever seen?
I don't think its an interesting story.
Re: Cutest bug you have ever seen?
Quote:
Originally Posted by (V|G)CC4ME
Code:
# include <iostream>
using namespace std;
int main()
{
float f[2][2] = {1.0, 2.0, 3.0, 4.0};
int i = (int)f[1, 1];
cout << i << endl;
}
Quote:
Originally Posted by (V|G)CC4ME
Oh, and as for the answer to my example: I'll leave it unsolved just to see what others think. What would the output be? Why does it compile?
Without reading other replies ...
1,1 evaluates to 1
f[1] evaluates to the address of the second row, i.e. &f[1][0]
That address is then cast to an int.
Quote:
Originally Posted by (V|G)CC4ME
What safer coding practice would have prevented this?
Use static_cast<int> instead of (int).
Re: Cutest bug you have ever seen?
One of the cutest bug I've seen was something like
Code:
// check if a is either 3 or 5
if (a == 3 | 5) {
And some of the cutest code (not a bug, works perfectly) I've seen was this (ok, it's JAVA, not C++):
Code:
for(int i=2;i<8;i++){
if(slotNumber.longValue() == i){
slotName = "Slot "+ (i-2);
}
}
Both pieces were written by "IT-professionals", not students.
Re: Cutest bug you have ever seen?
I must say I'm impressed guys. I've made so many bug's in my life I can't pick a favourite... :o
Re: Cutest bug you have ever seen?
For some reason, I like this one (access violation):
Code:
#include <iostream>
using namespace std;
char* numbers[10] = {
"One",
"Two",
"Three",
"Four",
"Five"
"Six",
"Seven",
"Eight",
"Nine",
"Ten"
};
int main()
{
for (int i = 0; i < 10; ++i)
{
std::cout << numbers[i] << " ";
}
return 0;
}
Re: Cutest bug you have ever seen?
Quote:
Originally Posted by Zachm
For some reason, I like this one (access violation):
Admittedly, I am not very familiar with the use of such global variables, but it seems to me that the problems with that example is not an access violation, but a missing semi-colon and that numbers should be an array of const char*.
EDIT:
Ah, the most obvious mistake is now corrected.
Re: Cutest bug you have ever seen?
Edited my last post.
Didn't try to compile it, but the semi-colon is not the problem I intended to demonstrate.
Disregard the fact that the array is global as well - this was just for demonstration purposes,
and look closer !
Re: Cutest bug you have ever seen?
hmm... after seeing your correction, I thought that perhaps I miscounted, but you have ten string literals in the array, and the array is of 10 char*, so where's the out of bounds access?
Re: Cutest bug you have ever seen?
Zachm, your code snippet just killed me. Awesome. I was staring at it trying to find a bug for like 15 minutes, until decided to run it and see. Great example :D
Re: Cutest bug you have ever seen?
Quote:
Zachm, your code snippet just killed me. Awesome. I was staring at it trying to find a bug for like 15 minutes, until decided to run it and see.
I ran it and did not get a seg fault... but I guess I was unlucky.
Yeah, I see the error now, but frankly, it was obscured by other mistakes :p