473,543 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

I 'm following various posting in "comp.lang. ada, comp.lang.c++ ,
comp.realtime, comp.software-eng" groups regarding selection of a
programming language of C, C++ or Ada for safety critical real-time
applications. The majority of expert/people recommend Ada for safety
critical real-time applications. I've many years of experience in C/C++ (and
Delphi) but no Ada knowledge.

May I ask if it is too difficult to move from C/C++ to Ada?
What is the best way of learning Ada for a C/C++ programmer?

Jul 23 '05
822 28943
Ioannis Vranos <iv*@remove.thi s.grad.com> wrote in news:1110072229 .85604
@athnrd02:
Jim Rogers wrote:
C:\c>temp
character: â¿
character: =
character: >
character:

252 61 62 0

00111111
10111100
01111100
00000000

C:\c>


All this can be done in Ada.

It would be great if we saw the code and its output.


Ok. That is certainly a fair requirement.

with Ada.Text_Io;
with Ada.Integer_Tex t_Io;
with System;

procedure String_Bits is
S : String := "This is a text message";
L : Natural := S'Size;
type Bits_Array is array (1..L) of Boolean;
pragma Pack (Bits_Array);
Bits : Bits_Array;
for Bits'Address use S'Address;
begin
-- Display the bits of each character
for I in Bits'range loop
Ada.Integer_Tex t_Io.Put(Item => Boolean'Pos(Bit s(I)), Width => 1);
if I mod System.Storage_ Unit = 0 then
Ada.Text_Io.New _Line;
end if;
end loop;
end String_Bits;

The output is:
00101010
00010110
10010110
11001110
00000100
10010110
11001110
00000100
10000110
00000100
00101110
10100110
00011110
00101110
00000100
10110110
10100110
11001110
11001110
10000110
11100110
10100110

The program declares a string S initialized to "This is a text message".
It then declares an array type named Bits_Array. That array type is an
array of Boolean, with the number of elements equal to the number of
bits in used by S. The variable L is set to equal the value returned
by the Size attribute of the String S. Ada reports size as the number
of bits, not the number of bytes.

A packed array of boolean allocates one storage element to each bit.
Therefore, the statement
pragma Pack(Bits_Array );
causes all instances of Bits_Array to be a packed array.
The variable Bits is declared to be of the type Bits_Array.
The next statement forces Bits to overlay S. Both variables
are the same size and start at the same address.

The body of the procedure simply iterates through all the bits in
the Bits variable and prints the numeric value of each bit.
A new line is output whenever the loop control variable mod
System.Storage_ Unit evaluates to 0. System.Storage_ Unit is
provided by Ada so that the program will properly represent
each storage unit (sometimes called a byte) no matter what the
size of that unit is.

As you can clearly see, Ada can represent the bits of a
variable with very little difficulty.

Ada does not store a null at the end of a string. This is
why there is no indication of a null value for the last byte.

Jim Rogers

Jul 23 '05 #71
Ioannis Vranos <iv*@remove.thi s.grad.com> wrote in
news:1110072660 .15684@athnrd02 :
Martin Dowie wrote:
It does NOT provide multi-tasking... or fixed-point numbers...

C++ provides whatever the system it is used to provides. In Windows
you can create multithreading applications for example.


Of course it can, by calling C libraries such as pThreads.

Concurrency has been a native part of Ada since the 1983 standard.


In general, we cannot compare the two languages because they have
different design ideals.

We we _can_ compare them... but you are correct that we must take
their design considerations into account. Ada95 was design
specifically to support efficient, real-time safe environments (the
OP of the original thread question), C++ was designed to produce an
object-orientated extension to C.

C++ is not only an OO language, but it is a multiparadigm one.

I agree. One of the original goals of C++ was to produce a language
that extends C into the OO paradigm without loosing any of the
inherited C capabilities. In 1996 C++ standardized generic programming
as another paradigm.

Ada is also a multi-paradigm language. In fact, it can be used in all
the paradigms familiar to C++.

Jim Rogers

Jul 23 '05 #72
"Ioannis Vranos" <iv*@remove.thi s.grad.com> wrote in message
news:1110055473 .613245@athnrd0 2...
You can also express constraints in templates. An important thing is this.
Are Ada's generics run-time or compile time?
Compile time with every Ada compiler I've used.
It looks like they are run-time, and in C++ you can do all the above.
Actually Ada's compilers are probably written in C++.


GNAT is written in Ada.

Jul 23 '05 #73
"Pascal Obry" <pa****@obry.or g> wrote in message
news:ub******** ***@obry.org...
At least GNAT (GNU/Ada) is written in Ada and DEC Ada was written in Ada
IIRC.


Half right. GNAT is written in Ada, DEC Ada was written in Bliss.
Jul 23 '05 #74
Jim Rogers wrote:
Of course it can, by calling C libraries such as pThreads.

Actually I am using .NET facilities (posted an example in another
message in the thread).

I agree. One of the original goals of C++ was to produce a language
that extends C into the OO paradigm without loosing any of the
inherited C capabilities. In 1996 C++ standardized generic programming
as another paradigm.

Ada is also a multi-paradigm language. In fact, it can be used in all
the paradigms familiar to C++.


OK.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #75
Jim Rogers wrote:
The program declares a string S initialized to "This is a text message".
It then declares an array type named Bits_Array. That array type is an
array of Boolean, with the number of elements equal to the number of
bits in used by S. The variable L is set to equal the value returned
by the Size attribute of the String S. Ada reports size as the number
of bits, not the number of bytes.

A packed array of boolean allocates one storage element to each bit.
Therefore, the statement
pragma Pack(Bits_Array );
causes all instances of Bits_Array to be a packed array.
The variable Bits is declared to be of the type Bits_Array.
The next statement forces Bits to overlay S. Both variables
are the same size and start at the same address.

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

The body of the procedure simply iterates through all the bits in
the Bits variable and prints the numeric value of each bit.
A new line is output whenever the loop control variable mod
System.Storage_ Unit evaluates to 0. System.Storage_ Unit is
provided by Ada so that the program will properly represent
each storage unit (sometimes called a byte) no matter what the
size of that unit is.

As you can clearly see, Ada can represent the bits of a
variable with very little difficulty.

Ada does not store a null at the end of a string. This is
why there is no indication of a null value for the last byte.

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*.
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).
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)?
Consider this:

#include <iostream>
#include <bitset>
#include <limits>

int main()
{
using namespace std;

double obj= 0.45435;

unsigned char *p= reinterpret_cas t<unsigned char *>(&obj);
p= reinterpret_cas t<unsigned char *>(&obj);
// Displays the decimal values of the
// individual bytes that obj consists of.
for(unsigned i=0; i<sizeof(obj); ++i)
cout<<static_ca st<unsigned>(p[i])<<" ";

cout<<"\n\n";
// Displays the bits of each byte that consist
// this SomeClass object
p= reinterpret_cas t<unsigned char *>(&obj);
for(unsigned i=0; i<sizeof(obj); ++i)
{
// A byte is not necessarily 8 bits
// numeric_limits< unsigned char>::digits retrieves the number
// of byte's bits, which *is* 8 usually.
bitset<numeric_ limits<unsigned char>::digits> bits(p[i]);

for(unsigned j=0; j<bits.size(); ++j)
cout<<bits[j];

cout<<"\n";
}
}
C:\c>temp
163 1 188 5 18 20 221 63

11000101
10000000
00111101
10100000
01001000
00101000
10111011
11111100

C:\c>

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #76
In article <Fs************ *********@news0 00.worldonline. dk>, "Peter Koch Larsen" <pk*****@mailme .dk> writes:
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.


Although I attempt to drive safely, I also rely on seat belts and air bags.
Jul 23 '05 #77
In article <d0**********@t itan.btinternet .com>, Martin Dowie <ma**********@b topenworld.com> writes:
Oh dear! That's a shocker!!!! Ada had generics back in Ada83 - and _all_
Ada83 compiler supported it then! The debugger support back then was
pretty shabby,


Speak for your own compiler, VAX Ada has (and had) excellent debugger
support.
Jul 23 '05 #78
Ioannis Vranos <iv*@remove.thi s.grad.com> wrote in
news:1110082147 .830198@athnrd0 2:
Jim Rogers wrote:
The program declares a string S initialized to "This is a text
message". It then declares an array type named Bits_Array. That array
type is an array of Boolean, with the number of elements equal to the
number of bits in used by S. The variable L is set to equal the value
returned by the Size attribute of the String S. Ada reports size as
the number of bits, not the number of bytes.

A packed array of boolean allocates one storage element to each bit.
Therefore, the statement
pragma Pack(Bits_Array );
causes all instances of Bits_Array to be a packed array.
The variable Bits is declared to be of the type Bits_Array.
The next statement forces Bits to overlay S. Both variables
are the same size and start at the same address.

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.
The body of the procedure simply iterates through all the bits in
the Bits variable and prints the numeric value of each bit.
A new line is output whenever the loop control variable mod
System.Storage_ Unit evaluates to 0. System.Storage_ Unit is
provided by Ada so that the program will properly represent
each storage unit (sometimes called a byte) no matter what the
size of that unit is.

As you can clearly see, Ada can represent the bits of a
variable with very little difficulty.

Ada does not store a null at the end of a string. This is
why there is no indication of a null value for the last byte.

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_Tex t_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.Mod ular_IO(Byte);

procedure Show_Bits(Item : Target_Type) is
Bit_View : Bits_Array(1..T arget_Size);
for Bit_View'Addres s use Item'Address;
Byte_View : Byte_Array(1..T arget_Size / Byte'Size);
For Byte_View'Addre ss 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_Tex t_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_Ty pe, 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_Bi ts(Mt);
Ada.Text_Io.Put _Line("Output of Long_Float");
Flt_Bits.Show_B its(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

Jul 23 '05 #79
On Sat, 5 Mar 2005 23:44:56 +0100, Peter Koch Larsen wrote:
"Ludovic Brenta" <lu************ @insalien.org> skrev i en meddelelse
news:87******** ****@insalien.o rg...

This is the crux of the problem. Assuming that the programmer "knows
the rules", "makes no mistakes" or "can be trusted" is a recipe for
disaster. One of the principles in Ada's rationale is to make
everything explicit, rather than implicit.


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.
type Weight is digits 8 range 0.0 .. 900.0; -- 8 decimal digits of
precision
type Length is digits 8 range 0.0 .. 1000.0;

Now these types are incompatible. If you want to mix them, you need
to define the semantics and provide the appropriate operators:

type Weight_Length is digits 8 range 0.0 .. 900_000.0;

function "*" (Left : in Weight; Right : in Length) return Weight_Length;

Since you don't provide "+", there is no way to add a weight to a
length.

For a more general discussion of physical quantities in Ada, see:

http://home.t-online.de/home/Christ-.../Universe.html


I believe I will prefer the C++ solution - a template that copes with all
this stuff.


Try to write a unit calculator using templates ...

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
Jul 23 '05 #80

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
2323
by: Mediocre Person | last post by:
Well, after years of teaching grade 12 students c++, I've decided to make a switch to Python. Why? * interactive mode for learning * less fussing with edit - compile - link - run - debug - edit - compile - link - run -..... * lots of modules * I was getting tired of teaching c++! Bored teacher = bad instruction.
14
1795
by: Gabriel Zachmann | last post by:
This post is not strictly Python-specific, still I would like to learn other university teachers' opinion. Currently, I'm teaching "introduction to OO programming" at the undergrad level. My syllabus this semester consists of a bit of Python (as an example of a scripting language) and C++ (as an example of a compiled language). With C++, I...
3
1524
by: andy_irl | last post by:
Hi there I have been asked to teach HTML to a group in our local village community. It is nothing too serious, just a community development grant aided scheme. It will be a 10 week course of two hours per week and will mainly consist of mature students. I may or may not include GUI's depending if I can fit it all in to the time allocated. ...
12
1974
by: Pierre Senellart | last post by:
I am going to teach a basic Web design course (fundamentals of HTML/CSS, plus some basic client-side (JavaScript) and server-side (PHP, perhaps XSLT) scripting). Most of the students do not have any previous knowledge of all of this. I am strongly considering teaching XHTML 1.0 Strict instead of HTML 4.01 strict, for the following reasons:...
16
4355
by: msnews.microsoft.com | last post by:
I am teaching C# to my 11 year old child. One challenge is that all the C# books I own and that I have seen in bookstores are full of language that is not easily comprehended by a student at that age. Can anyone recommend books (or perhaps websites) tuned for younger audiences? BTW, its amazing how fast a student can absorb this kind of...
24
2828
by: Richard Aubin | last post by:
I'm really new to vb.net programming and programming in general. I would like to teach myself on how to program effectively and I have the financial and time resources to do so. Can I anyone recommend and point me in the right direction where I should start? -- Richard Aubin
0
1701
by: e.expelliarmus | last post by:
check this out buddies. kool website for: * hacking and anti hacking tricks * anti hackng tricks. * registry tweaks * orkut tricks * small virus * computer tricks and loads of different tricks... www.realm-of-tricks.blogspot.com www.registrydecoded.blogspot.com
1
3884
by: JosAH | last post by:
Greetings, Introduction This week's tip describes a few old tricks that are almost forgotten by most people around here. Sometimes there's no need for these tricks anymore because processors nowadays are so fast and memory comes in abundance. But still, if we implement an algorithm that is better, or more efficient, than another one,...
0
7408
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7688
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5885
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5271
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3391
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
636
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.