Nasser Abbasi wrote:
> Geoff Bull wrote:
> > Of course, with Ada, we don't have to "assume" anything,
> > and we don't have to write programs that "usually" work.
> > And writing truly portable code is generally not that hard.
> yes, offcourse Ada is much more portable than C/C++. but still in Ada,
> doesn't the size of an 'integer' for example depend on the platform on
> is on?
From section 3.5.4 of the Ada 95 reference manual:
"In an implementation, the range of Integer shall include the range
-2**15+1 .. +2**15-1. "
Any implementation I have seen has had a 32 bit Integer.
Obviously an application that depends on a 32 bit Integer is non-portable
- but at least either the compiler or the run time check will indicate there
is a problem.
With Ada you don't get silence while numbers are being truncated or
having their sign changed, as happens in C, C++ and Java.
> Java is even more portable than Ada, becuase in Java, all primitive data
> types have the same size on any platform. offcourse this is easy to do
> since the platform is the same on every OS, which is the JVM :)
And they naively specified the answer must be the same on all platforms.
This creates problems for numerics.
E.g. An implementation is not allowed to take advantage of 80 bit FP
on Intel platforms to give a more accurate result.
Also not allowed to use a multiply-add instruction that avoids intermediate
rounding - this can be a 50% performance hit, and may not give an
answer with the best accuracy possible for the platform
In Java, portability is forced on you.
You can't take adavantage of the features of a particular machine,
even if you want to.
In C/C++, portability is difficult.
Ada gives you the facilities to easily write portable code,
and the option to write non-portable code where that is desirable.
The Ada compiler will help you out when you take your non-portable code
to another platform, rather than giving wrong answers like C/++.
> but it is nice to know that an int is 32 bits, no matter where one is
> running the program, and that a long is 64 bits no matter where, etc..
Actually writing a binding to the Java interface provides an interesting
of C and Ada portability.
As you say a Java int is always 32 bits.
The C binding has
typedef int jint;
which will break on any machine where int is 16, 36, 64 bits etc.
This declaration is in the machine dependent header file.
/* jni_md.h contains the machine-dependent typedefs for jbyte, jint
and jlong */
For Ada we would write
type Jint is range -2**31 .. 2**31 - 1;
for Jint'Size use 32;
And this will work on any platform where the Ada compiler
can handle 32 bit numbers.
Or we could shoot ourselves in the foot and write
type Jint is new Interfaces.C.int;
and that, while more like the C code, would be non-portable (like the C