Results 1 to 6 of 6

Thread: weird overflow error...

  1. #1

    Thread Starter
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727

    weird overflow error...

    hi all, could someone tell me why:
    VB Code:
    1. Dim x As Integer, a As Byte, b As Byte
    2. a = 0
    3. b = 1
    4. x = a - b
    generates an overflow error on last line, while:
    VB Code:
    1. Dim x As Integer, a As Byte, b As Byte
    2. a = 0
    3. b = 1
    4. x = a + b
    does not. i have to modify first operation into:
    VB Code:
    1. Dim x As Integer, a As Byte, b As Byte
    2. a = 0
    3. b = 1
    4. x = a * 1 - b * 1
    to make it work. this is really weird.

    thanks,

    wc.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  2. #2
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    In Front of my computer...
    Posts
    367
    change data type... maybe
    Born to help others
    (If I've been helpful then please rate my post. Thanks)

    call me EJ or be slapped!

  3. #3

    Thread Starter
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727
    that's the whole point... x IS an integer value.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    The problem is that by default all calculatations are performed in the smallest data type that is appropriate, and as both variables are bytes then the calculation is done in bytes.

    However, if you look in MSDN for the limits of the byte data type:

    Data type &nbsp; Storage size &nbsp; &nbsp; &nbsp; &nbsp; Range
    Byte &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 byte &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 to 255

    and -1 is outside of the valid range for the data type.


    The "* 1" you added to make it work fixes this by doing an Implicit data type conversion, an explicit one would be better (as the compiler can change what method it uses rather than doing extra work), eg:

    x = CInt(a) - CInt(b)

  5. #5

    Thread Starter
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727
    agreed on the fact that the overflow is generated by the fact that 0 - 1 = -1 is outside the range of bytes. agreed also on the implicit conversion:

    -> that's why i've added the *1 multiplication in the first place (and you are right, better do an explicit conversion).

    however, the fact is that x IS AN INTEGER!

    i've declared it as such, therefore i still can scarcely understand why this is generating an error. i believe this is a small memory management leak in vb6, assuming to compute on the smallest type even if the user (me) has correctly allowed memory space according on presumed results.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Where the results are going has no effect whatsoever on how the calculation is performed

    Even though it is only one line of code, there are several steps that take place "behind the scenes". Essentially the calculation is done first (which is where the error occurs), and then the result is returned to the required location (eg: your x variable).

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