CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 5 1234 ... LastLast
Results 1 to 15 of 64
  1. #1
    Join Date
    Oct 2008
    Posts
    15

    Cutest bug you have ever seen?

    Hey guys, this is my first post here. What better way to introduce myself than with an interesting programming story?

    I'm a student currently participating as a programmer for FIRST Robotics Competition Team 339. In my three years on the team I have learned almost every nuance of C++ imaginable, from boolean operators to template metaprogramming.

    One of the my most interesting encounters with obscure C++ syntax, however, occurred very recently, at a competition a few weeks ago. Someone from another team who is somewhat new to C++ showed me code similar to the following and asked why it compiled but gave him bogus results:

    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;
    }
    I just had to chuckle when I saw this one. I'm not sure what's worse: the fact that someone had written this code or the fact that I took one look at it and immediately knew what the problem was. I now refer to this example as the "cutest bug of all time".

    So, I thought I'd ask others on this site: what do you think is the "cutest" C++ bug you have ever seen? Where have you seen that perfect combination of simplicity and obscurity that means you just have to laugh when you discover the solution?

    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? What safer coding practice would have prevented this?

    I'm hoping this will turn out to be an interesting topic. Enjoy!

  2. #2
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Cutest bug you have ever seen?

    No, this is not a interesting story.


    The integer after decimal is just ignored by integer.

    Please correct me if wrong.
    Thanks for your help.

  3. #3
    Join Date
    Oct 2008
    Posts
    15

    Re: Cutest bug you have ever seen?

    Quote Originally Posted by Peter_APIIT
    No, this is not a interesting story.


    The integer after decimal is just ignored by integer.

    Please correct me if wrong.
    Try it and see.
    Last edited by (V|G)CC4ME; October 19th, 2008 at 09:55 PM.

  4. #4
    Join Date
    Nov 2007
    Posts
    87

    Re: Cutest bug you have ever seen?

    Integers can't store decimal numbers, so the stuff after the decimal point is just truncated.
    If you change i to a floating point number, then this truncation won't happen.

  5. #5
    Join Date
    Nov 2007
    Posts
    87

    Re: Cutest bug you have ever seen?

    Oh, and he is actually converting a float* to an integer...

  6. #6
    Join Date
    Oct 2008
    Location
    Pune, India
    Posts
    54

    Re: Cutest bug you have ever seen?

    1,1 evaluates to 1 in C/C++. Thus, f[1,1] is equal to f[1].
    The print statement is equal to
    Code:
    cout << (int)f[1] << endl;
    A conversion from pointer to int.

    In Fortran, you access array elements as f(1,1). In C/C++, you need to use f[1][1].

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Cutest bug you have ever seen?

    Unless you have overloaded the comma operator.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Oct 2008
    Posts
    15

    Re: Cutest bug you have ever seen?

    Yes, that's it. The array dereferencing syntax is wrong - he should have typed [1][1] instead of [1, 1]. The funny thing is, this compiled without problem thanks to the explicit (int) cast. A static_cast<int>() would have been a better choice.

    Why do I call it the cutest bug of all time? Because this code compiled through a bizarre combination of circumstances:

    1. The guy writing it forgot how to dereference a two-dimensional array - that should normally be a syntax error.

    2. The guy's best guess turned out to be something very different but completely legal in C++ (the comma operator) - okay, but its still a compile error due to the illegal pointer conversion.

    3. The guy was already casting to int, which incidentally erased all traces of the illegal conversion.

    After looking at the code, this was my explanation of the problem: "you see this comma in the square brackets? In this context, it is known as the comma operator, and it was included in the language to annoy you when you accidentally used it."

    Does anyone else have an example of a similar "cute" bug they have seen?

    (BTW, I find it funny that two members offered me a nice explanation of floating point truncation before noticing the real problem. )

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Cutest bug you have ever seen?

    Quote Originally Posted by (V|G)CC4ME
    .... In this context, it is known as the comma operator, and it was included in the language to annoy you when you accidentally used it....
    Not at all, the comma operator is critical to certain operations.

    And overloading it is one of the most efficiant ways to perform complex initializations......
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  10. #10
    Join Date
    Nov 2007
    Posts
    87

    Re: Cutest bug you have ever seen?

    Why isn't this code working?

    Code:
    #define x 9;
    
    int main()
    {
    	int i = 0;
    	while (i++ < x)
    		cout << i << endl;
    	return 0;
    }

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Cutest bug you have ever seen?

    Extra semicolon!

    That one took a moment.

  12. #12
    Join Date
    Oct 2008
    Location
    Pune, India
    Posts
    54

    Re: Cutest bug you have ever seen?

    You mean "not compiling"? ";" after the 9 in #define.

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Cutest bug you have ever seen?

    For my own part, just recently I spotted a for loop where I was trying to iterate over two lists (of different lengths) at once. Comma operator in the initializer clause. Comma operator in the increment cause. And because everything *had* to be aligned nicely to look uncluttered......yup, you guessed it, I had a comma operator in the termination condition too....

  14. #14
    Join Date
    Oct 2008
    Posts
    15

    Re: Cutest bug you have ever seen?

    Quote Originally Posted by Lindley
    For my own part, just recently I spotted a for loop where I was trying to iterate over two lists (of different lengths) at once. Comma operator in the initializer clause. Comma operator in the increment cause. And because everything *had* to be aligned nicely to look uncluttered......yup, you guessed it, I had a comma operator in the termination condition too....
    LOL, that's a good one. Would have been even better if those termination conditions had side effects.

  15. #15
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: Cutest bug you have ever seen?

    Something which happened to me when I was learning C++:

    Code:
    string s = getStringFromUser();
    
    //iterate through string backwards
    for(size_t pos = s.length() - 1; pos >= 0; --pos) {
       cout << s[pos];
    }
    Took me hours to find the error.

    And another one was when I mixed up i and j counter variables in nested for loops. Not funny.

    One more it took me LONG time to find the error:
    Code:
    #include <iostream> 
    #include <string> 
    #include <vector>
    #include <numeric>
    #include <algorithm>
    
    using namespace std;
    
    int main() 
    { 
    	vector<unsigned long long> vec;
    	vec.push_back(123456789012);
    	vec.push_back(987654321098);
    
    	unsigned long long result = accumulate(vec.begin(), vec.end(), 0);
    	cout << result;
    }
    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 (And no, you won't get it, TheCPUWizard. Let beginners have some fun )

    Cheers
    B+!
    'There is no cat' - A. Einstein

    Use &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

Page 1 of 5 1234 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured