Strange behaviour in different environments

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ahammad
    New Member
    • May 2007
    • 79

    Strange behaviour in different environments

    Hello, I have a small program that takes in a number and performs some operations to that number, then returns the value. The relevant code is here:

    Code:
          CString codeStr;  
          unsigned __int64 code = (((unsigned __int64)atoi(m_ActEdit) * 9999) + 5) % 1234;
    m_ActEdit is an entry in an edit box. I'm using atoi to convert it to an int.

    After it has been converted to an int it goes through that series of operations to output a number.

    Here is the issue. I have that compiled using Visual Studio 6 in XP and using Visual Studio 2005 in Vista.

    I used the same input number: 4102182706

    The results that I got were different when I ran that same number in both programs:

    VS6/XP = 177
    VS2005/Vista = 1102

    I'm not sure as to why this is happening...Has there been any changes to the way some of the code works (like atoi)? I don't think that the OS has anything to do with it.
  • Darryl
    New Member
    • May 2007
    • 86

    #2
    Is one of your machines using a 64 bit processor?

    also instead of using atoi, try using _strtoui64

    Comment

    • Benny the Guard
      New Member
      • Jun 2007
      • 92

      #3
      I think Darryl is right. The issue is that your # 4102182706 is 32 bits long. atoi returns an int which on most compiler (even 64-bit) is a 32 signed int, meaning you only have 31 bits. So you lose some there. I bet if you use a smaller number you would get he same results.

      So you need to use the 64-bit atoi to convert your # to be safe and consistent with your return value.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Also keep in mind that atoi() and it's family of functions are deprecated in C++.

        Comment

        • ahammad
          New Member
          • May 2007
          • 79

          #5
          Thanks, that works for number that are 19 digits or less.

          So I'm assuming that a 64 bit integer is a signed 20 digit number, so an unsigned 19 digit number. Is this right?

          Comment

          Working...