[xpost] a new C/C++ type that when overflow i see it

A

Alexei A. Frounze

I have done one too.




It isn't.


You can write a correct C program that doesn't use floating point.
You can use a nonconforming C implementation
to translate and execute it.

q14.3 of the clc FAQ acknowledges that some implementations
of C, only link the math library as an option, not by default.

Btw, 20.6b to which refers 14.3 is wrong:
-----8<-----
For example, here is a ``careful'' addition function:
int
chkadd(int a, int b)
{
if(INT_MAX - b < a) {
fputs("int overflow\n", stderr);
return INT_MAX;
}
return a + b;
}
-----8<-----
How about b = -1, a = INT_MIN being inputs of this "carefully" adding
function?
The above looks like the implementation for unsigned addends that was
turned into signed addition w/o much or any thinking by simply
changing unsigned int's to int's and UINT_MAX to INT_MAX. The correct
implementation would be:

if (b >= 0)
{
if (INT_MAX - b < a)) {
fputs("int overflow\n", stderr);
return INT_MAX;
}
}
else
{
if (INT_MIN - b > a)) {
fputs("int overflow\n", stderr);
return INT_MIN;
}
}
return a + b;

Alex
 
¬

¬a\\/b

Nice idea, integers that kick if abused.
Implement in C++ and see if the C++ operator overloading feature is all it
is cracked up to be.

In C the idea is unusable, because you need an explicit call for every
operation,

it seems yes; i need to define a function for each type or explicity
say what function to call using cast
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top