Discussion:
Overflow
(too old to reply)
Olav
2008-10-27 20:46:15 UTC
Permalink
Hi,

In the code below, should not a decent compiler at least give the programmer
a warning in a situation like this?
Ooops, if I haven't been on Mister Zale's unwanted customers list before; I
surely will be now. The sentence above is all it takes.
--
Olav

#COMPILE EXE "C:\Test"
#DIM ALL
#DEBUG ERROR ON
#TOOLS ON
#COMPILER PBCC 4.04
'---------------------------------------------------------------------------------------------------------------------------------------
FUNCTION PBMAIN () AS LONG
LOCAL a AS BYTE
LOCAL b AS LONG

ON ERROR GOTO ErrorHandler

b = 256
a = b 'a equals 0, but the compiler does not issue a warning nor an
error about the overflow

? "Value of a is:" a

? "Press a key to end the program..."
WAITKEY$

Leave:
EXIT FUNCTION
ErrorHandler:
? "Error:" + ERROR$(ERRCLEAR)
RESUME Leave
END FUNCTION
Tom Lake
2008-10-28 02:51:14 UTC
Permalink
Post by Olav
Hi,
In the code below, should not a decent compiler at least give the
programmer a warning in a situation like this?
Ooops, if I haven't been on Mister Zale's unwanted customers list before;
I surely will be now. The sentence above is all it takes.
You have to have the correct compiler options set to detect conditions
such as array reference out of bounds, overflow, etc. Once your program
is debugged, you can remove those switches and see how eliminating
all that checking really speeds up your program.

Tom Lake
Olav
2008-10-28 06:43:04 UTC
Permalink
Post by Tom Lake
Post by Olav
Hi,
In the code below, should not a decent compiler at least give the
programmer a warning in a situation like this?
Ooops, if I haven't been on Mister Zale's unwanted customers list before;
I surely will be now. The sentence above is all it takes.
You have to have the correct compiler options set to detect conditions
such as array reference out of bounds, overflow, etc. Once your program
is debugged, you can remove those switches and see how eliminating
all that checking really speeds up your program.
Tom Lake
Which options are you thinking of?
Isn't #DEBUG ERROR ON and ON ERROR..... meant to catch up on errors?
--
Olav
Tom Lake
2008-10-28 12:31:13 UTC
Permalink
Post by Olav
Post by Tom Lake
You have to have the correct compiler options set to detect conditions
such as array reference out of bounds, overflow, etc. Once your program
is debugged, you can remove those switches and see how eliminating
all that checking really speeds up your program.
Tom Lake
Which options are you thinking of?
Isn't #DEBUG ERROR ON and ON ERROR..... meant to catch up on errors?
Yes but with the compiler switches not set, there IS no error on overflow.
The value only wraps around. Using an 16-bit integer variable gives this:

32767 + 1 = error if compiler switch set, 0 if not set.

Tom Lake
Michael Mattias
2008-10-28 13:15:04 UTC
Permalink
Post by Tom Lake
32767 + 1 = error if compiler switch set, 0 if not set.
Not with the PB/Windows compilers (PB/Windows or PB/CC) . There is no
overflow or underflow error detection available.

The PB/DOS compilers DO offer overflow detection.


MCM
Olav
2008-10-28 15:58:24 UTC
Permalink
Post by Tom Lake
Post by Olav
Post by Tom Lake
You have to have the correct compiler options set to detect conditions
such as array reference out of bounds, overflow, etc. Once your program
is debugged, you can remove those switches and see how eliminating
all that checking really speeds up your program.
Tom Lake
Which options are you thinking of?
Isn't #DEBUG ERROR ON and ON ERROR..... meant to catch up on errors?
Yes but with the compiler switches not set, there IS no error on overflow.
Sorry, but I'm not able to catch up on what yoy are talking about.
Are you thinking of something else than PB's Windows Console compiler?
--
Olav
Tom Lake
2008-10-28 16:40:13 UTC
Permalink
Post by Olav
Post by Tom Lake
Post by Olav
Post by Tom Lake
You have to have the correct compiler options set to detect conditions
such as array reference out of bounds, overflow, etc. Once your program
is debugged, you can remove those switches and see how eliminating
all that checking really speeds up your program.
Tom Lake
Which options are you thinking of?
Isn't #DEBUG ERROR ON and ON ERROR..... meant to catch up on errors?
Yes but with the compiler switches not set, there IS no error on overflow.
Sorry, but I'm not able to catch up on what yoy are talking about.
Are you thinking of something else than PB's Windows Console compiler?
PBCC doesn't check for overflow at all. I was thinking of the DOS version that lets
the
programmer decide if overflow should be detected.

Tom Lake
Frank Cox
2008-10-28 19:16:44 UTC
Permalink
Post by Tom Lake
Yes but with the compiler switches not set, there IS no error on
overflow. The value only wraps around. Using an 16-bit integer
32767 + 1 = error if compiler switch set, 0 if not set.
Though I can't think of any examples off of the top of my head and I
haven't personally used the technique anywhere, I remember reading that
there are legitimate uses for that behaviour (wrapping on overflow) in
some applications.

So in some cases, it's an error that's not really an error.
Michael Mattias
2008-10-28 20:55:54 UTC
Permalink
Post by Frank Cox
Post by Tom Lake
Yes but with the compiler switches not set, there IS no error on
overflow. The value only wraps around. Using an 16-bit integer
32767 + 1 = error if compiler switch set, 0 if not set.
Though I can't think of any examples off of the top of my head and I
haven't personally used the technique anywhere, I remember reading that
there are legitimate uses for that behaviour (wrapping on overflow) in
some applications.
So in some cases, it's an error that's not really an error.
It's not an "error" or "not an error" - it's a behavior, pure and simple;
deal with it.

However, be aware that what is supported here is not "wrapping", it is,
"when you overflow the results are undefined."

Looks like in this particular case it "wrapped" but you may not rely on that
being always true.

MCM
Frank Cox
2008-10-28 21:45:01 UTC
Permalink
Post by Michael Mattias
However, be aware that what is supported here is not "wrapping", it is,
"when you overflow the results are undefined."
Now that is interesting and something I didn't know. Mind you, I never
looked into it and I don't have or use PB for Windows. I don't actually
have Windows on any of my computers. But it's interesting that you can't
depend on a wrap but might get some possibly random result instead.

I don't know enough about this issue to know if this is common behaviour
for most compilers (Basic and otherwise) or if you can usually depend on
a wrap. As I stated earlier, I have read that some programs depend in
some way on this behaviour so it must be predictable and repeatable with
at least some compilers. Apparently the wrap in the example posted here
is repeatable but you say the behaviour is also unpredictable for other
values -- that would make it difficult to depend on that as a function in
any program.

Michael Mattias
2008-10-27 23:47:02 UTC
Permalink
Post by Olav
Hi,
In the code below, should not a decent compiler at least give the
programmer a warning in a situation like this?
Ooops, if I haven't been on Mister Zale's unwanted customers list before;
I surely will be now. The sentence above is all it takes.
--
There is no overflow or underflow checking the in Windows compilers. It's
in the help file.

It's also on a new feature suggestion I sent in four releases ago.

If you don't think it's a decent compiler, A) you are mistaken (subjective
though that judgement may be); B) use something else.
--
Michael C. Mattias
Tal Systems Inc.
Racine WI
***@talsystems.com
Olav
2008-10-28 15:28:09 UTC
Permalink
Post by Michael Mattias
Post by Olav
Hi,
In the code below, should not a decent compiler at least give the
programmer a warning in a situation like this?
Ooops, if I haven't been on Mister Zale's unwanted customers list before;
I surely will be now. The sentence above is all it takes.
--
There is no overflow or underflow checking the in Windows compilers. It's
in the help file.
It's also on a new feature suggestion I sent in four releases ago.
If you don't think it's a decent compiler, A) you are mistaken (subjective
though that judgement may be); B) use something else.
--
Michael C. Mattias
Tal Systems Inc.
Racine WI
Maybe overflow wasn't the right term to use here.As you know overflow can
occur as a result of arithmetic operations.
What I showed here was not of that kind, but rather a situation where one
datatype -which can hold small numbers - were assigned a datatype, which
can hold larger numbers
A good compiler ought to warn the programmer about this.

BTW, I'm perfectly able to decide for myself which compiler I should use.
Thank you!
--
Michael Mattias
2008-10-28 15:34:33 UTC
Permalink
Post by Olav
Post by Michael Mattias
Hi,
There is no overflow or underflow checking the in Windows compilers.
It's in the help file.
Maybe overflow wasn't the right term to use here.As you know overflow can
occur as a result of arithmetic operations.
Um, call me an old fuddy-duddy, but trying to assign a value greater than
the capacity of the target data type sure *sounds* like an "overflow error."

MCM
Loading...