F
fmdf
Alberto said:3. Many of you want us to believe that ADa performs ra nge checking
without loss of performance: it can be true at compile time with fixed
ranges, but it can't definitely be done without chechinkg array bounds
every time data is accessed if we don't know these bounds before
compiling (e.g.: typical cases where dynamic allocation of memory is
used)
Do you think C++ is more performing at range checking? When you don't
anymore need checkings in a Ada program you can use "Pragma
Suppress()". What can you do in a C++ program? I suppose you can only
rewrite that code.
Suppose you want a class type to hold a value in range -100 .. 100, I
think you have to code a lot to build a class that can't be mistakenly
used to hold absolut greater values.
Please compare the following two simple programs each of them have a
bug overflowing a value. I am sure that they can be better coded, or
that better solutions can be implemented as I'm not either a C++ or Ada
professional programmer, yet the C++ code is not as expressive and
compact as the Ada counterpart.
// over.cpp
#include <iostream>
using std::cout;
using std:
class Range_Error
{
public:
Range_Error()
: message( "raised \"Out of Range\"" ) {}
const char* what() const { return message; }
private:
const char* message;
};
class Var_T
{
public:
Var_T( const int& i = int() )
{
assign( i );
}
Var_T& operator++( )
{
assign( ++Var );
return *this;
}
bool assign( const int& i )
{
if ( i >= 0 && i <= 100 )
Var = i;
else throw Range_Error();
return true;
}
friend std:
obj )
{
out << obj.Var;
return out;
}
private:
int Var;
};
int main()
{
Var_T Var( 0 );
try
{
for ( int i = 0; i <= 100; ++i ) // range check error expected
++Var;
cout << Var << '\n';
}
catch( Range_Error ERR )
{
cout << ERR.what() << '\n';
}
return 0;
}
-- over.adb
with Ada.Text_IO;
use Ada.Text_IO;
procedure Over is
-- Pragma Suppress( Range_Check );
type Var_T is range 0 .. 100;
package Var_IO is new Ada.Text_IO.Integer_IO( Var_T );
Var : Var_T := 0;
begin
for Index in 0 .. 100 loop -- range check failing is expected
Var := Var + 1;
end loop;
Var_IO.Put ( Var );
New_Line;
exception
when Constraint_Error =>
Put_Line ( "Constrained Error" );
when others =>
Put_Line ( "Unknown Error" );
end Over;
Ada example is less than half longer than C++, it is most expressive,
it catches all exceptions (not only out of range) and it suppresses all
range checks when uncommenting "Pragma".
What dou you think of this kind of Ada unefficient and too many lines
of code example?
Ciao,
fabio de francesco