|
-
October 20th, 2008, 02:54 AM
#16
Re: Cutest bug you have ever seen?
 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! 
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?
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
October 20th, 2008, 03:07 AM
#17
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
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
October 20th, 2008, 03:08 AM
#18
Re: Cutest bug you have ever seen?
I guess the accumulate issue was some time ago Hobson? This is captured by the compiler today.
-
October 20th, 2008, 03:16 AM
#19
Re: Cutest bug you have ever seen?
well the compiler gives a warning now
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
October 20th, 2008, 03:32 AM
#20
Re: Cutest bug you have ever seen?
I got stumped on this one for a while when I first learnt C. 
Code:
// Oops, infinite loop.
unsigned char c;
for (c = 0; c < 256; c++)
{
/// Do stuff.
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
October 20th, 2008, 03:39 AM
#21
Re: Cutest bug you have ever seen?
I don't think its an interesting story.
-
October 20th, 2008, 04:00 AM
#22
Re: Cutest bug you have ever seen?
 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;
}
 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.
 Originally Posted by (V|G)CC4ME
What safer coding practice would have prevented this?
Use static_cast<int> instead of (int).
-
October 20th, 2008, 04:54 AM
#23
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.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
October 20th, 2008, 05:18 AM
#24
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...
-
October 20th, 2008, 06:24 AM
#25
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;
}
Last edited by Zachm; October 20th, 2008 at 06:36 AM.
-
October 20th, 2008, 06:34 AM
#26
Re: Cutest bug you have ever seen?
 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.
Last edited by laserlight; October 20th, 2008 at 06:39 AM.
-
October 20th, 2008, 06:38 AM
#27
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 !
-
October 20th, 2008, 06:40 AM
#28
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?
-
October 20th, 2008, 06:41 AM
#29
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
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
October 20th, 2008, 06:42 AM
#30
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.
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|