Arithmetic overflow checking

M

MikeP

Eric said:
In C the array size is not part of the type or value, so there is
nothing to check.

No; the size (element count) is part of an array's type. Your
compiler will confirm this for you by issuing a diagnostic for

char matrix[5][7]; /* five char[7] arrays */
char (*nine)[9]; /* pointer to char[9] */
nine = matrix; /* point it at the first char[7] */
C integer arithmetic is always modulo M, for some large M (like
2**32 or 2**64). So the concept of overflow does not apply.

This is true only for `unsigned' integer arithmetic. Signed
integer arithmetic is in fact vulnerable to overflow.

And if you take "overflow" to mean "can do something undesireable (wrap)
without warning", so does unsigned arith. To say "So the concept of
overflow does not apply" seems to attempt to dismiss the behavior as
being OK somehow instead of recognizing the deficiency that it is most of
the time.
 
M

MikeP

BartC said:
Even if zero-overhead detection was possible, it's difficult to know
how to make use of this in C. For example:

int a,b,c;

c=a+b;

The a+b overflows, but then what?

Then you fix the bug in the program that causes the overflow.
You can't then magically switch
over to:
long long int a,b,c;

Even /with/ the overhead, it's difficult to see what could follow
such an expression:

if (overflow(c=a+b)) ...

In the context of C-based code for implementing auto-ranging, dynamic
types of /another language/, this might be workable, but still
difficult to see how it can be done with zero-overhead. But this is a
limited application (which I wouldn't even attempt in C because it's
so fiddly).
Aborting a program is also a possibility, but this just helps in
debugging, and overheads are less relevant.

"JUST" helps debugging? Wouldn't it be nice to get thos MANY, COMMON bugs
out of code before it gets deployed?
 
M

MikeP

Patricia said:
Write the application in Ada.

Patricia

But C# is very Java-like and has "checked" and also the compiler-level
equivalent, so C# would be the better alternative. (And yes, I do know
you were just kidding about Ada).
 
M

MikeP

Peter said:
To be clear, I don't think _Tom_ needs or doesn't need this. He was
simply replying to the OP.


I don't disagree with that. In fact, I'm skeptical that in any
program it really makes sense to apply overflow checking to _every_
computation, even not counting the issues that have already been
pointed out wrt the JDK itself and other libraries which rely on the
lack of it. For example, does every integral "for" loop _really_
need the index increment operation to be checked for overflow? Seems
unlikely to me.

It's Java. Why worry about performance?! And how expensive really is it?
Instead of just increment, increment and check the hardware overflow
flag. (Oops, did I say "hardware" in a Java NG? My bad).
While the discussion has been interesting, I can't help but feel that
the original question is fundamentally flawed. The answers are mostly
academic, as it's unlikely anyone really ought to be trying to
approach the problem in this way in the first place.

It's as easy as setting a compiler switch in some languages (not in C or
C++ though, but C#, e.g.), so the approach seems sound and desireable for
a language to implement. The programmer shouldn't be overly burdened with
the task of overflow checking.
Well, I think Tom's suggestion of some sort of automatic code-rewriter
_could_ be the best solution in _certain_ scenarios. Such an
implementation could be applied selectively to specific code.

I would not worry about the "simple" or "efficient" criteria. IMHO,
if one is deciding to apply overflow checking to every computation,
one has already abandoned the hope of efficiency.

I don't think that is true. It depends on the application of course, but
for most programs, wouldn't a single-digit performance hit be acceptable?
(OK, maybe not on already slow Java ;) ).

Frankly, as long as the discussion is purely hypothetical (as it is
now), I don't see how anyone can claim with 100% certainty the broad
superiority or inferiority of any given solution. It's too dependent
on the specifics of whatever real-world scenario is at hand.

I think the issue has achieved critical mass (programmers, not just comp
scientists) now and every language needs to "keep up with the Joneses"
(C#) to stay relevant.
 
M

Malcolm McLean

if i say something
if you have a prog that 'can' fail
the exception that make end the program is right
because this point out one error to correct;

if you have a prog can not fail
it is better not raise the exception
Yes, sometimes you just have to plough on regardless, and hope that
the system will recover.
 
L

lewbloch

Patricia said:
No, I was not really joking, though I did not attempt to find all the
languages that would meet the stated requirement.

Others did not think you were joking. I've known people who use Ada
professionally and not one complained about the language. I've only
ever encountered disparagement of Ada from people who don't use it.

I've never worked with Ada, so I defer to those who have.
I'm very strongly of the opinion different languages should provide
different features, making different trade-offs, and programmers should
pick the language for a job based on its requirements and those features.

The alternative a lot of programmers follow seems to be to pick one
language, ignore all the others, and then complain when there is a
mismatch between that language's features and their current requirements.

I have no problem with pushing minor changes and additional features
within the general framework of a language, but if the basic framework
is not a good match for a job, the solution is to pick a language that
is more suitable.

+1
 
G

Gene Wirchenko

Then you fix the bug in the program that causes the overflow.

First, you have to detect the overflow. Since the language does
not make that easy, you may miss it.

[snip]

Sincerely,

Gene Wirchenko
 
M

MikeP

Gene said:
First, you have to detect the overflow. Since the language does
not make that easy, you may miss it.

To me, he seemingly implied that the overflow WOULD be detected after the
addition in his example and that he was asking how to handle it.
 
M

MikeP

Patricia said:
No, I was not really joking, though I did not attempt to find all the
languages that would meet the stated requirement.

Don't look now, but if you weren't joking, then you recommended Ada to a
Java programmer! Oh my.
I'm very strongly of the opinion different languages should provide
different features, making different trade-offs, and programmers
should pick the language for a job based on its requirements and
those features.

You have to admit, it's quite a chasm between Java/C# and Ada.
The alternative a lot of programmers follow seems to be to pick one
language,

I do/did that. (C++ is my poison).
ignore all the others,

I have regularly looked at other languages and used them in minor ways
for evaluation.
and then complain when there is a
mismatch between that language's features and their current
requirements.

In another post, I said that I think that today (like in right now) the
awareness of the overflow issue (language support) has achieved critical
mass. Combine that with the alternatives that are available and more yet
to come, a language cannot afford to go the path of, say, C anymore for
it will lose relevance much more quickly. It's not complaining. It's
customer feedback (companies BEG their customers for such!). Companies
that don't recognize their customers needs and change with the times, go
out of business. Java is not C and can't afford to stagnate like C did
(OK, C++ gave it a "reconditioning"), or it won't last.
I have no problem with pushing minor changes and additional features
within the general framework of a language, but if the basic framework
is not a good match for a job, the solution is to pick a language that
is more suitable.

C# will fit in a lot of places where Java does (or so I assume given what
I know about them, as I'm don't use either language other than for
evaluation and case study). Pushing away programmers to other languages
instead of evolving the language according to the expectations (i.e.,
what programmers have come to expect to be standard feature in a given
class of language) is surely a path to obsolescence.
 
J

John B. Matthews

"MikeP" <[email protected]> said:
Patricia Shanahan wrote: [...]
No, I was not really joking, though I did not attempt to find all
the languages that would meet the stated requirement.

Don't look now, but if you weren't joking, then you recommended Ada
to a Java programmer! Oh my.

I often suggest Ada to Java programmers; knowledgeable Java programmers
often return the favor; I've learned a lot that way.
You have to admit, it's quite a chasm between Java/C# and Ada.

I find points of comparison very illuminating. Perhaps "chasm" is a
matter of perspective.
I do/did that. (C++ is my poison).

See also: "The science of fanboyism."
Article: <http://techreport.com/discussions.x/21294>
Discussion: <http://science.slashdot.org/story/11/07/15/1331243>
 
L

lewbloch

John said:
MikeP said:
Patricia Shanahan wrote: [...]
No, I was not really joking, though I did not attempt to find all
the languages that would meet the stated requirement.
Don't look now, but if you weren't joking, then you recommended Ada
to a Java programmer! Oh my.

Oh, your what? What are you acting so shocked about? Give us logic,
evidence, reasoning, not just superficial rhetorical devices. What in
bloody blazes is so strange about recommending Ada to a Java
programmer, hm?

Nothing!

Let's be an engineer, "MikeP", hm-k?
I often suggest Ada to Java programmers; knowledgeable Java programmers
often return the favor; I've learned a lot that way.

Be specific. No one has to admit that. As the person making the
claim, the burden of proof is on you, "MikeP". Demonstrate your
point, please. Define "chasm", how to measure it, and what makes the
difference "quite" a chasm.
I find points of comparison very illuminating. Perhaps "chasm" is a
matter of perspective.

Perhaps "quite a chasm" is a matter of someone wanting to sound
impressive who has no facts or reasoning behind their argument, so
they make little unsupportable foolish comments full of "nudge, nudge,
wink, wink" instead.

Not the good ones, nor the ones who wish to stay employed.

C'mon, "MikeP", this is a programmers' group. Give us more than
tabloid gossip-column rhetoric, please.
 
L

lewbloch

MikeP said:
C# will fit in a lot of places where Java does (or so I assume given what
I know about them, as I'm [sic] don't use either language other than for

You might want to know a little more about the languages before
rendering judgment.

C# and Java run on different platforms - C# far fewer than Java. What
do you mean by "a lot"?
evaluation and case study). Pushing away programmers to other languages
instead of evolving the language according to the expectations (i.e.,
what programmers have come to expect to be standard feature in a given
class of language) is surely a path to obsolescence.

"Surely"? Would you mind providing *any* evidence or logic for that
claim?

Over three decades as a professional programmer, I've had to know a
zillion languages to avoid obsolescence. Those who stuck with Fortran
when C became popular found themselves marginalized with frightening
speed, although of course there is still Fortran work out there. C
programmers had to know shell programming and assembler to get any
work done. If you don't know SQL, you are so screwed. If you stayed
with C when C++ and # and Java came out, you were dooming yourself to
obsolescence, over the large segments of the market. C++ has become
ivory tower and very competitive - unless you're one of the best in
that world, failing to learn other languages "surely" doomed you to
obsolescence. C# and Java are similar in a lot of ways, but language
alone doth not make the program. Software is 1% programming and 99%
deployment and operations. They run in different environments, and
you cannot really use either one without SQL, HTML, Javascript and
things like Python, and now, of course, JQL and other metalanguages.
Don't speak XML? Hello, obsolescence! Can't use JSON? You're
limited. Can't read bytecode? You're less of a Java programmer.

You couldn't be more mistaken in your conclusions. A programmer needs
to know a minimum of two full-fledged programming languages, one with
the ability to create wild pointers, a database-query language, a
shell and at least one scripting language just to be minimally
competent.

Please don't imagine that you're going to impress anyone if you
respond to this post with more of your indirect, suggestive comments
unsubstantiated by even the merest jot of reasoning and evidence.
 
M

MikeP

Patricia said:
MikeP said:
Patricia Shanahan wrote:
MikeP wrote:
Patricia Shanahan wrote:
MikeP wrote:
Patricia Shanahan wrote:
Write the application in Ada.

But C# is very Java-like and has "checked" and also the
compiler-level equivalent, so C# would be the better
alternative. (And yes, I do know you were just kidding about
Ada).

No, I was not really joking, though I did not attempt to find all
the languages that would meet the stated requirement.

Don't look now, but if you weren't joking, then you recommended
Ada to a Java programmer! Oh my.

Huh? Java, like any healthy programming language, is a tool, not a
religion.

Just like a dentist's drill and an oil well [sic] drill are tools?

Yes, exactly. Both of those are tools, and neither of those are
religions.

And it would be just as pointless to complain about the dental drill
taking too long to drill a thousand foot well as to complain about the
oil well drill being too big to fit in a patient's mouth. It's much
better to look at the job and pick the tool that is most suitable for
it.

I found it bizarre that you recommended an oil well drill to a dentist.
 
M

MikeP

lewbloch said:
John said:
MikeP said:
Patricia Shanahan wrote: [...]
No, I was not really joking, though I did not attempt to find all
the languages that would meet the stated requirement.
Don't look now, but if you weren't joking, then you recommended Ada
to a Java programmer! Oh my.

Oh, your what? What are you acting so shocked about?

That Patricia recommended "an oil well drill to a dentist". I did not
think the concept was obtuse.
Be specific. No one has to admit that. As the person making the
claim, the burden of proof is on you, "MikeP". Demonstrate your
point, please. Define "chasm", how to measure it, and what makes the
difference "quite" a chasm.

You are on a witch-hunt so I'll bid you farewell (as in buh-bye, I don't
do pee-pee measuring contests).
Perhaps "quite a chasm" is a matter of someone wanting to sound
impressive who has no facts or reasoning behind their argument, so
they make little unsupportable foolish comments full of "nudge, nudge,
wink, wink" instead.

Don't look now but your EQ (lack of) is showing.
C'mon, "MikeP", this is a programmers' group. Give us more than
tabloid gossip-column rhetoric, please.

I really have no advice to those who are proverbial "bit heads" other
than maybe enroll in a few Humanities classes?

Now, if you want to grow up and have some post-adolescent discourse,
fine. Else just puh-leeeze, go away!
 
A

Andreas Leitgeb

MikeP said:
Patricia said:
:
Patricia Shanahan wrote:
MikeP wrote:
Patricia Shanahan wrote:
MikeP wrote:
Patricia Shanahan wrote:
Write the application in Ada.

But C# is very Java-like and has "checked" and also the
compiler-level equivalent, so C# would be the better
alternative. (And yes, I do know you were just kidding about
Ada).

No, I was not really joking, though I did not attempt to find all
the languages that would meet the stated requirement.

Don't look now, but if you weren't joking, then you recommended
Ada to a Java programmer! Oh my.

Huh? Java, like any healthy programming language, is a tool, not a
religion.

Just like a dentist's drill and an oil well [sic] drill are tools?


Yes, exactly. Both of those are tools, and neither of those are
religions.

And it would be just as pointless to complain about the dental drill
taking too long to drill a thousand foot well as to complain about the
oil well drill being too big to fit in a patient's mouth. It's much
better to look at the job and pick the tool that is most suitable for
it.
I found it bizarre that you recommended an oil well drill to a dentist.

That's just, because the dentist expressed a need to exploit the oil well
in his backyard.
 
M

MikeP

Andreas said:
MikeP said:
Patricia said:
On 7/16/2011 11:00 AM, lewbloch wrote:
:
Patricia Shanahan wrote:
MikeP wrote:
Patricia Shanahan wrote:
MikeP wrote:
Patricia Shanahan wrote:
Write the application in Ada.

But C# is very Java-like and has "checked" and also the
compiler-level equivalent, so C# would be the better
alternative. (And yes, I do know you were just kidding about
Ada).

No, I was not really joking, though I did not attempt to find
all the languages that would meet the stated requirement.

Don't look now, but if you weren't joking, then you recommended
Ada to a Java programmer! Oh my.

Huh? Java, like any healthy programming language, is a tool, not
a religion.

Just like a dentist's drill and an oil well [sic] drill are tools?


Yes, exactly. Both of those are tools, and neither of those are
religions.

And it would be just as pointless to complain about the dental drill
taking too long to drill a thousand foot well as to complain about
the oil well drill being too big to fit in a patient's mouth. It's
much better to look at the job and pick the tool that is most
suitable for it.
I found it bizarre that you recommended an oil well drill to a
dentist.

That's just, because the dentist expressed a need to exploit the oil
well in his backyard.

He did not. He wanted to provide better care to his patients.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top