Teaching new tricks to an old dog (C++ -->Ada)

  • Thread starter Turamnvia Suouriviaskimatta
  • Start date
J

Jim Rogers

Does this mean that Boolean always occupies 1 bit and has no padding
bits?

No. A boolean usually occupies a byte. A packed array of boolean
is compressed so that each boolean element of the array occupies only
one bit.
While this is an interesting thing, I have the feeling that this
approach does not print all bits, including padding bits, of a
*user-defined type*.

The same approach will work for any type, including any padding
bits. The Size attribute reports the total number of bits used
by an object.
In C++ you can read (and thus copy, print or anything) every byte of
any type.

In the example you provided, I have the feeling that you allocated a
character array (the string) and then treated is a boolean array
(somewhat a hacking attempt to imitate the behaviour).

I created a string, which is an array of character in Ada.
I also created an packed array of boolean with exactly the same
size as the array of character. I then specified that both
variables will occupy the same space; one overlays the other.
However what happens in the case of a user defined type (I suppose Ada
supports OO programming) or a record. Can you print the byte
implementation of such an object?


Also for a built in type, say a floating point, can you print its
implementation bytes too (including padding bits)?

The following example starts with the creation of a generic package
for printing the byte and bit output of an object of any type.
The program then instantiates that package for a user-defined
type and for a long_float, which is equivalent to a C++ double.

generic
type Target_Type is private;
Target_Size : Natural;
package Bit_Utils is
procedure Show_Bits(Item : Target_Type);
end Bit_Utils;


with Ada.Text_Io;
with Ada.Integer_Text_Io;
with System;
package body Bit_Utils is
type Bits_Array is array(Positive range <>) of Boolean;
pragma Pack(Bits_Array);

type Byte is mod 2**8;
type Byte_Array is array(Positive range <>) of Byte;
package Mod_Io is new Ada.Text_IO.Modular_IO(Byte);

procedure Show_Bits(Item : Target_Type) is
Bit_View : Bits_Array(1..Target_Size);
for Bit_View'Address use Item'Address;
Byte_View : Byte_Array(1..Target_Size / Byte'Size);
For Byte_View'Address use Item'Address;
begin
for I in Byte_View'range loop
Mod_Io.Put(Item => Byte_View(I), Width => 4);
end loop;
Ada.Text_IO.New_Line(2);
for I in Bit_View'range loop
Ada.Integer_Text_Io.Put(Item => Boolean'Pos(Bit_View(I)),
Width => 1);
if I mod System.Storage_Unit = 0 then
Ada.Text_IO.New_Line;
end if;
end loop;
end Show_Bits;
end Bit_Utils;

with Bit_Utils;
with Ada.Text_IO;

procedure Bit_Output is
type My_Type is record
Name : String(1..4);
Age : Positive;
Weight : Long_Float;
end record;
package My_Bits is new Bit_Utils(My_Type, My_Type'Size);
package Flt_Bits is new Bit_Utils(Long_Float, Long_Float'Size);

Mt : My_Type := ("Jim ", 55, 0.45435);
D : Long_Float := 0.45435;
begin
Ada.Text_Io.Put_Line("Output of My_Type");
My_Bits.Show_Bits(Mt);
Ada.Text_Io.Put_Line("Output of Long_Float");
Flt_Bits.Show_Bits(D);
end Bit_Output;


The output of this program is:

Output of My_Type
74 105 109 32 55 0 0 0 163 1 188 5 18 20 221 63

01010010
10010110
10110110
00000100
11101100
00000000
00000000
00000000
11000101
10000000
00111101
10100000
01001000
00101000
10111011
11111100
Output of Long_Float
163 1 188 5 18 20 221 63

11000101
10000000
00111101
10100000
01001000
00101000
10111011
11111100


Ada provides capabilities similar to C++ in the area of copying
and viewing data.

Jim Rogers
 
D

Dmitry A. Kazakov

"Ludovic Brenta" <[email protected]> skrev i en meddelelse


Do you also require parenthesis when mixing addition and multiplication? I
do not like that degree of nannying - a matter of taste, surely.

That's the Ada's way. The following is also illegal without brackets:

+ - 2
A**+2

Though they might look unambiguous, no sensible man would write something
like above.

As for addition and multiplication. The association rules of */+ differs
from ones of and/or:

x + (y * z) is not equivalent to (x + y) * (x + z)

So there is a canonic representation of algebraic expressions with */+. For
and/or the picture is different:

x and (y or z) = (x and y) or (x and z)
(x and y) or z = (x or z) and (y or z)

There is no dedicated representation: a normal disjunctive form is as good
as a normal conjunctive one.
I believe I will prefer the C++ solution - a template that copes with all
this stuff.

Try to write a unit calculator using templates ...
 
I

Ioannis Vranos

Martin said:
But Ada is used for system programming language as well. Only it is not
painfull. Most C/C++ programmers think that a language suitable for system
programming must be painfull - but this is not true.

Well, there is a drawback: you have to type more. But then: once you typed
it in it's easer to read - and most programms are read more then written.

The real difference is that Ada puts emphasis on "save" while C++ puts
emphasis on "fast". C++ only becomes save by use of the STL.

And with the speed of the computers today "save" is better. I remember well
the time when Blaster/32 infected my computer faster then I could download
the fix (download the fix with Linux in the end - but that's another
story).

That is indeed true. But the differences are not system vs application -
both languages draw even here. Its save vs. fast, readabiltiy vs.
writeablity and explicid vs implicid.

Of corse, having stessed the save vs. fast point: Most Ada compiler allow
you to switch off the savety net with a compiler option.

Ada supports the same 4 paradigms and concurrent programming on top.

True. But C/C++ havn't got a monopoly/patent on that feature.

Actualy Ada has templates since it's 1983 release. I don't think C++ had
templates at the time.

True Ada is easy - but not restricted. You think one excludes the other -
but that isn't true.

Ada can do more low level stuff then C++ - or have you ever seen 24 bit
integer types in C++.

As I said, here you as mistaken. While Ada is indeed well suited for high
level application development is is mostly used for low level embedded
programming. Usualy in planes, railways, spacecraft and weapons.

I usualy read - at least - the wiki article before I say anything about a
programming language:

http://en.wikipedia.org/wiki/Ada_programming_language


Certainly after the thread discussion, ADA has ascended a lot in my
interest scale (I always try to be reasonable).


In that Wiki link I saw it is covering a subset of the procedural
paradigm and recently also got support for OO. Also it supports generics.

The one remaining, does it support namespaces? :) What subset of the
procedural paradigm it does not support?


Also I saw that under severe constraints the run-time safety of the
language is better to be switched off.


I do not know much on the language, but can one define general-purpose
containers with Ada's generics that do range checking and throw an
exception when there is an attempt to access outside the boundaries of
the container, even if the aforementioned run-time safety is switched off?
 
P

Peter Koch Larsen

Larry Kilgallen said:
Although I attempt to drive safely, I also rely on seat belts and air
bags.

I hope we all do. What i meant to say was that using a seat belt isn't an
excuse to drive on the wrong side of the road.

/Peter
 
M

Martin Krischik

Peter said:
It is also possible to use zero-terminated strings in C++, but every
knowledgeable person will advice that you use std::string. So what is the
difference here?

The defaults (that is the i.E. the constant expression "Hello Word!"):

Default in Ada are save strings - zero terminated via extenral library
(Interfaces.C.Strings).

Default in C++ is zero terminated, save strings via external library
(std::string).

Martin
 
D

Dmitry A. Kazakov

I hope we all do. What i meant to say was that using a seat belt isn't an
excuse to drive on the wrong side of the road.

Yes, in Britain :)-)) Ada compiler will ask you to apply
Unchecked_Conversion to the road sides, before you will be able to run the
engine ...
 
M

Martin Krischik

Peter said:
It is not a question of prevention. C++ allows you to do some really
dangerous things - in my mind rightfully so.

So does Ada. In fact Ada.Unchecked_Convertion will convert variables no C++
compiler will ever dare to convert. Yes I know, you can allways use a
reinterpret_cast <void*> in the middle.

http://en.wikibooks.org/wiki/Programming:Ada:Subtypes#Unchecked_Conversion
When you do program in C++
you must "constrain yourself" and not use the dangeous stuff - unless you
need to do so, of course ;-)

In Ada you don't need to constrain yourself. I found that a very freeing
exprience - after 10+ years of C/C++ programming. And I can still use the
dangeous stuff when I need to.

And see the difference in our sentences here:

You: contrain, not use, unless - all so negative terms.
Me: don't contrain, still use, when - so more positive terms.
It's kind of like driving. You can get into a lot of trouble if you do not
follow the traffic laws (you just have to obey the physicla ones ;-), but
so long as you do driving is a fairly safe practice.

Have you ever seen the traffic in a country where a 10 $ or 10 ¤ note will
make any traffic policemen put is pen back into its pocket.

Martin
 
M

Martin Krischik

<veröffentlicht & per Mail versendet>
I think the fallacy of that statement has been proven already (in a very
expensive way).

You mean the case where some managers decided to use some software written
for one pice of hardware on another - incompatible - pice of hardware -
without retesting?

In my book that was a management bug - If the managers had ordered to run
the testsuite only once the problem would have shown. The hardware was so
incompatible it would have failed all the time.

Last not least: Runtime check where disabled for that incident. So if
anything: this incident speak in favor of runtime checks.

Read up your facts: http://en.wikipedia.org/wiki/Ariane_5_Flight_501

Martin
 
M

Martin Krischik

C++ provides whatever the system it is used to provides.

If it is not in the ISO/IEC 14882 than it is not provided by the language
but only plugged on externaly.
In Windows you
can create multithreading applications for example.

I have a multi-tastking Ada application which runs on OS/2, Windows XP and
Linux. Can you do that in C++ without the use of "#if". Here is the problem
of plugged on solution: they are not compatible between operating system
and compiler vendors.

And even if you find a lib who can all three - I just ask a Mac-OS and
Solaris user on comp.lang.ada to run a test - The Ada multithreading is
part of the ISO/IEC 8652 standart - you recompile it on the next system and
it runs.
C++ is not only an OO language, but it is a multiparadigm one.

So is Ada only with a few more paradigms build in and not plugged onto by
external libraries.

Again "build in" means defined by "ISO/IEC" - everything else does not count
when comparing languages.

Martin
 
M

Martin Krischik

Ioannis said:
Martin Krischik wrote:
Certainly after the thread discussion, ADA has ascended a lot in my
interest scale (I always try to be reasonable).

If your interest has been raised there is a larger Ada article on Wikibooks:

http://en.wikibooks.org/wiki/Programming:Ada
In that Wiki link I saw it is covering a subset of the procedural
paradigm and recently also got support for OO. Also it supports generics.
The one remaining, does it support namespaces? :) What subset of the
procedural paradigm it does not support?

Yes, they are called packages and have been in Ada since 1983.

http://en.wikibooks.org/wiki/Programming:Ada:Packages

However they are move powerfull. For example generics are based on packages,
a bit like:

template said:
Also I saw that under severe constraints the run-time safety of the
language is better to be switched off.

Which I never do. With activated optimizer the performance loss is so low
there is almost no point in switching run-time check of.
I do not know much on the language, but can one define general-purpose
containers with Ada's generics that do range checking and throw an
exception when there is an attempt to access outside the boundaries of
the container, even if the aforementioned run-time safety is switched off?

Shure, there are several container libs around, all doing that.

http://en.wikibooks.org/wiki/Programming:Ada:Libraries:Container

I have to confess that only the new Ada 2005 will have containers as a build
in features. With the current Ada 95 you have to use external libraries.

Martin
 
L

Leif Roar Moldskred

Ioannis Vranos said:
The one remaining, does it support namespaces? :) What subset of the
procedural paradigm it does not support?

Yes, of course.
I do not know much on the language, but can one define general-purpose
containers with Ada's generics that do range checking and throw an
exception when there is an attempt to access outside the boundaries of
the container, even if the aforementioned run-time safety is switched
off?

Yes, but there's no point, as turning off the automatic boundary
checks and then do manual boundary checks instead doesn't gain you
anything except pain. (In fact, the automatic boundary checks can be
more efficent since compilers might optimize away checks that aren't
needed.)
 
M

Martin Krischik

there is also one other thing i can think of: template specialization.
this is an interresting construct, used a lot in C++, which gives the
ability to write "traits". there may be a way to do it in Ada but i'm
not aware of it (if there is, please tell me).

Yes traits would be tricky - but not impossible. Ada allows for both
procedures and packages as generic formal parameter:

http://www.adaic.org/standards/95lrm/html/RM-12-6.html
http://www.adaic.org/standards/95lrm/html/RM-12-7.html

However there are two important differences here which would make traits a
typing feast in Ada:

1) Ada generics work on procedures and packages but not classes. A bit like
template <...> namespace X {};

2) Ada genenrics only instanciate explicitly. Well it's a hole namespace
with all classes, prodedures, types and data after all.

But as I said, not impossible. I have a regex generic which can be
instanciated for character and wide character strings and basicly any other
descreed type you want to run regular expressing over.

With Regards

Martin
 
M

Martin Krischik

Peter said:
If you disregard the controversial "export" keyword, most recent compilers
are close to being 100% conforming (but might not be so "out-of-the-box").

This is a good one: All Ada templates are export since 1983 and most C++
compiler still can't do it.

Same for the dynamic arrays in C99 which Ada also had since 1983.
Do you also require parenthesis when mixing addition and multiplication? I
do not like that degree of nannying - a matter of taste, surely.

No! See http://adaic.org/standards/95lrm/html/RM-4-5.html.

Martin
 
P

Paul E. Bennett

Martin said:
<veröffentlicht & per Mail versendet>


You mean the case where some managers decided to use some software written
for one pice of hardware on another - incompatible - pice of hardware -
without retesting?

In my book that was a management bug - If the managers had ordered to run
the testsuite only once the problem would have shown. The hardware was so
incompatible it would have failed all the time.

Last not least: Runtime check where disabled for that incident. So if
anything: this incident speak in favor of runtime checks.

Read up your facts: http://en.wikipedia.org/wiki/Ariane_5_Flight_501

Yes, I have read that and the full report. However, I think my comment
still stands. You stated "when the compiler cannot check some code
statically, it inserts run-time checks which are guaranteed to catch all
errors by raising exceptions". The code had to be compiled, for the new
hardware, to be installed in the guidance system and hence should have had
the run-time checks in place if static checking could not be done. You have
admitted in your response that these checks were not active (by management
decision) so the Ada compiler was circumvented and prohibited from adding
these checks.

Considering that the proposition in this thread has been "Ada protects you
from making silly mistakes" I consider that your take is counter to the
evidence. I still maintain that language is immaterial to the safety of the
system, relying on decent rigourously applied development processes,
reviews and testing. Therefore, I tend to look at the development processes
and their "real" CMM rating. I guess the Ariane team went down a few
notches on that project.

--
********************************************************************
Paul E. Bennett ....................<email://[email protected]>
Forth based HIDECS Consultancy .....<http://www.amleth.demon.co.uk/>
Mob: +44 (0)7811-639972
Tel: +44 (0)1235-811095
Going Forth Safely ....EBA. http://www.electric-boat-association.org.uk/
********************************************************************
 
P

Paul E. Bennett

Larry said:
Although I attempt to drive safely, I also rely on seat belts and air
bags.

It has often been commented that a sharp spike protruding from the centre
of the steering wheel will elicit more safe driving than seatbelts or
airbags. Getting used to having a safety net will tend to lead to the less
cautious approach.

--
********************************************************************
Paul E. Bennett ....................<email://[email protected]>
Forth based HIDECS Consultancy .....<http://www.amleth.demon.co.uk/>
Mob: +44 (0)7811-639972
Tel: +44 (0)1235-811095
Going Forth Safely ....EBA. http://www.electric-boat-association.org.uk/
********************************************************************
 
M

Mark Lorenzen

[snip]
Certainly after the thread discussion, ADA has ascended a lot in my
interest scale (I always try to be reasonable).

I will give you some street respect for this and also for the fact
that the thread is actually informative and hasn't evolved into a
flamewar.

Regards,
- Mark Lorenzen
 
L

Ludovic Brenta

Ed said:
There are certainly other strategies available. For instance, in an
"integrated modular avionics" architecture, an unhandled Ada
exception in a single partition could be forwarded to a global
health monitoring facility that may restart that partition, a set of
partitions, or the whole system - or do something else for error
recovery. This implies that exception propagation is a quite
flexible capability, and can be embedded in a system with even
greater error handling flexibility in a comfortable way.

The software I'm currently working on is the "boot", or "BIOS" of our
hardware. It allows us to upload an operating system onto the target
board. The OS is then responsible for the partitioning that you
describe. So, inside the "boot" software, we have absolutely nothing
we can use to propagate exceptions. Every exception results in some
processor registers being set and a "jump" instruction to a fixed
address. We can use the processor registers to easily find the point
where the exception was raised, but we cannot handle it.

In another project I worked on, there was only one partition, and
every exception would result in the watchdog restarting the software.

The mechanism you describe is indeed one possible solution for
software that runs on top of an operating system.
 
L

Larry Kilgallen

I hope we all do. What i meant to say was that using a seat belt isn't an
excuse to drive on the wrong side of the road.

No, but despite always intending to drive on the proper side of the road,
I still wear a seat belt. There might be an unintended incident, such as
a bee stinging me, in which case I want to be as safe as possible.
 
X

xpyttl

I think Modula-3 was the prime inspiration but never having used it I
could be wrong! :)

One of the most fascinating computing documents ever written was the
"Rationale for the Design of the ADA programming Language" SIGPLAN Notices
6/79 (and yes, ADA was capitalized in that particular title, although if I
recall, in a later edition is was not.)

It is quite unusual to see such a complete treatment of what people were
thinking when they came up with a language. Anyway, in the introduction:

"The main source of inspiration for the Green language is the programming
language Pascal and its later derivatives."

However, throughout the document, they reference Algol-60 and Algol-68 much
more frequently than Pascal. That may make sense as Algol was seen as the
inspiration for Pascal, so many of Pascal's features appeared there first.
They also frequently make comparisons to Simula. In a quick scan, I didn't
see any reference to Modula, let alone Modula-3 which came a lot later -
1992 maybe?. Interestingly enough, Modula-2 also seems to have surfaced in
1979, and talks about safety-critical systems. (I get the impression there
never was a just plain Modula).

If you can get hold of a copy of the Rationale, it is a very intresting
read.

...
 
P

Pete Fenelon

In comp.realtime xpyttl said:
1979, and talks about safety-critical systems. (I get the impression there
never was a just plain Modula).

There was, we used an elderly Modula compiler for real-time courses in
the late 80s. It was essentially Pascal with separate compilation, a
process abstraction (though I can't remember whether this was in the
language or achieved entirely through libraries), and proper abstract
data types. Modula-2 moved somewhat further from the Pascal roots (I
think mainly to make it a systems-programming language for the Lilith
workstation) then the paths forked. Modula-3 became a much bigger language;
Wirth got small with Oberon, which was a very compelling language (and
environment) that never really achieved critical mass.

pete
 

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,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top