CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 5 FirstFirst 12345 LastLast
Results 16 to 30 of 64
  1. #16
    Join Date
    Jun 2008
    Posts
    592

    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!
    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

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

    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 &#91;code] [/code] tags!

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

  3. #18
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Cutest bug you have ever seen?

    I guess the accumulate issue was some time ago Hobson? This is captured by the compiler today.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #19
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    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."

  5. #20
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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

  6. #21
    Join Date
    Jul 2008
    Posts
    24

    Re: Cutest bug you have ever seen?

    I don't think its an interesting story.

  7. #22
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    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).
    My hobby projects:
    www.rclsoftware.org.uk

  8. #23
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    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.

  9. #24
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  10. #25
    Join Date
    Oct 2006
    Posts
    616

    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.

  11. #26
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    Last edited by laserlight; October 20th, 2008 at 06:39 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #27
    Join Date
    Oct 2006
    Posts
    616

    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 !

  13. #28
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

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

    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 &#91;code] [/code] tags!

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

  15. #30
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Page 2 of 5 FirstFirst 12345 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