Live data from Hacker News

Ada for the C++ and Java Developer [pdf]

learn.adacore.com

11–20 of 72 posts

Re: Ada for the C++ and Java Developer [pdf]

#11

Does Ada have real memory safety features? Or is it of the "better than nothing" C++ kind? For example: are array out-of-bounds checked? Or prevented at compile time? What about overflow? ...

In addition to what other's have mentioned in direct response to the examples you mentioned, there are additional safety factors - both by default and opt-in.

Pointers - accesses in Ada parlance - have rules that help ensure you can't have an invalid access. For one, the object itself has to be declared 'aliased' to create accesses to it. There are also rules to do things like ensure you can't create an access to some type at a higher scope than the type itself.

There are also memory pools that you can use. So you can specify that all allocations of a type occur in a specific pool or sub-pool. In addition to letting you control how allocation occurs, you can also use them for memory safety. All items in a sub-pool will be freed when the pool falls out of scope, so you can simply leave deallocations to occur that way.

There are also concurrency tools baked-in, with runtime support at least. Specifically in terms of memory safety, you have protected objects. They will automatically ensure you have a single writer at a time, and will do other nice things like still allow multiple readers. If you need to, you can get a fair bit of control over how it all works.

Re: Ada for the C++ and Java Developer [pdf]

#12

If any Ada users are here, I have question on one section: procedure Main is type Distance is new Float; type Area is new Float; D1 : Distance := 2.0; D2 : Distance := 3.0; A : Area; begin D1 := D1 + D2; -- OK D1 := D1 + A; -- NOT OK: incompatible types for "+" operator A := D1 * D2; -- NOT OK: incompatible types for ":=" assignment A := Area (D1 * D2); -- OK end Main; > The predefined Ada rules are not perfect; they…

There is now, at least with Ada 2012 and GNAT[0][1]. While I'm pretty sure only GNAT has implemented it, I'm not sure if it's actually in the 2012 standard or not.

I haven't played with it yet, but it does looks pretty interesting.

[0]https://gcc.gnu.org/onlinedocs/gcc-4.9.4/gnat_ugn_unw/Perfor... [1]https://blog.adacore.com/uploads/dc.pdf

Re: Ada for the C++ and Java Developer [pdf]

#13
post #9

If any Ada users are here, I have question on one section: procedure Main is type Distance is new Float; type Area is new Float; D1 : Distance := 2.0; D2 : Distance := 3.0; A : Area; begin D1 := D1 + D2; -- OK D1 := D1 + A; -- NOT OK: incompatible types for "+" operator A := D1 * D2; -- NOT OK: incompatible types for ":=" assignment A := Area (D1 * D2); -- OK end Main; > The predefined Ada rules are not perfect; they…

I got it working with: with Ada.Text_IO; use Ada.Text_IO; procedure Main is type Distance is new Float; type Area is new Float; function "*" (Left, Right : Distance) return Area is temp : Distance; begin temp := Left * Right; return Area (temp); end "*"; D1 : Distance := 2.0; D2 : Distance := 3.0; A : Area; begin A := D1 * D2; -- OK Put_Line(A'Image); end Main; Interestingly, it took me several attempts to create thi…

Could you do

    return Area(Float (Left) * Float (Right));

Re: Ada for the C++ and Java Developer [pdf]

#14
post #4

Earlier quoted context omitted.

Yes they are bounds checked, at compile time, or runtime if not able to prove them at compile time. Overflow is checked. However both can be disabled via unsafe code pragmas if so desired. As of Ada 2012, the SPARK proof system was integrated into Ada and you can also use DbC as formal proofs. Many of the use cases that in C++ would require new/delete are handled by the compiler itself, thus there is an error if when…

Although Ada still isn't fully memory safe. Read-before-write causes undefined behaviour, if I recall correctly.

Accesses - pointers - are automatically nulled when declared. So you won't silently screw up who-knows-what with an uninitialized access, though yeah, it's not perfectly safe.

Re: Ada for the C++ and Java Developer [pdf]

#15
post #13
post #9

Earlier quoted context omitted.

I got it working with: with Ada.Text_IO; use Ada.Text_IO; procedure Main is type Distance is new Float; type Area is new Float; function "*" (Left, Right : Distance) return Area is temp : Distance; begin temp := Left * Right; return Area (temp); end "*"; D1 : Distance := 2.0; D2 : Distance := 3.0; A : Area; begin A := D1 * D2; -- OK Put_Line(A'Image); end Main; Interestingly, it took me several attempts to create thi…

Could you do return Area(Float (Left) * Float (Right));

Yes, that works.

Re: Ada for the C++ and Java Developer [pdf]

#16

If any Ada users are here, I have question on one section: procedure Main is type Distance is new Float; type Area is new Float; D1 : Distance := 2.0; D2 : Distance := 3.0; A : Area; begin D1 := D1 + D2; -- OK D1 := D1 + A; -- NOT OK: incompatible types for "+" operator A := D1 * D2; -- NOT OK: incompatible types for ":=" assignment A := Area (D1 * D2); -- OK end Main; > The predefined Ada rules are not perfect; they…

I am not an Ada expert, but I came up with this. Please notice that the idea here is not to have the compiler do proper dimensional analysis but simply to avoid the default operator overloading of "*" for the Distance type that also returns a Distance value.

Nothing very high-level, as we need to explicitly manipulate the single-component records, but it does the job.

    with Ada.Text_IO; use Ada.Text_IO;
    
    procedure Main is
       type Distance is record
          Value : Float;
       end record;
    
       type Area is record
          Value : Float;
       end record;
    
       function "*" (Left, Right : Distance) return Area is
       begin
          return (Value => Left.Value * Right.Value);
       end "*";
    
       D1 : constant Distance := (Value => 10.0);
    
       D2 : constant Distance := (Value => 20.0);
    
       -- D3 : constant Distance := D1 * D2;
       -- Does not compile. GNAT returns the following error:
       --   main.adb:21:33: expected type "Distance" defined at line 4
       --   main.adb:21:33: found type "Area" defined at line 8
    
       A : constant Area := D1 * D2;
    begin
       Put_Line ("Area A is " & Float'Image(A.Value));
    end Main;*

Re: Ada for the C++ and Java Developer [pdf]

#17
post #7

If any Ada users are here, I have question on one section: procedure Main is type Distance is new Float; type Area is new Float; D1 : Distance := 2.0; D2 : Distance := 3.0; A : Area; begin D1 := D1 + D2; -- OK D1 := D1 + A; -- NOT OK: incompatible types for "+" operator A := D1 * D2; -- NOT OK: incompatible types for ":=" assignment A := Area (D1 * D2); -- OK end Main; > The predefined Ada rules are not perfect; they…

It does have operator overloading so you could certainly define some function "*"(Left, Right : Distance) return Area and if dedicated enough build up a whole set of unit-types with corresponding conversion rules. Not an Ada guy at all though so no clue if there's a less by-hand way of doing it.

The Units library does exactly what you describe. http://www.dmitry-kazakov.de/ada/units.htm

Re: Ada for the C++ and Java Developer [pdf]

#18

Earlier quoted context omitted.

Although Ada still isn't fully memory safe. Read-before-write causes undefined behaviour, if I recall correctly.

Accesses - pointers - are automatically nulled when declared. So you won't silently screw up who-knows-what with an uninitialized access, though yeah, it's not perfectly safe.

I didn't mean pointers/access-types, I meant ordinary integer-type locals.

Re: Ada for the C++ and Java Developer [pdf]

#19
I used Ada (first Ada95, later Ada2005) while working on the GPS program, before leaving aerospace and taking on work using Java.

I found the Java type system to be horrendous in comparison. Much better employment potential, though...

Re: Ada for the C++ and Java Developer [pdf]

#20
post #8
post #4

Earlier quoted context omitted.

Yes they are bounds checked, at compile time, or runtime if not able to prove them at compile time. Overflow is checked. However both can be disabled via unsafe code pragmas if so desired. As of Ada 2012, the SPARK proof system was integrated into Ada and you can also use DbC as formal proofs. Many of the use cases that in C++ would require new/delete are handled by the compiler itself, thus there is an error if when…

In addition Ada allows dynamically-sized arrays to be allocated on the stack and to be returned from a function. Efficient implement of the latter requires rather non-trivial support on the compiler side and C/C++/Rust have nothing like that. Yet in many cases it allows to eliminate new/delete and simplify code. For example, just consider if C allowed to return a plain C string from a function without any heap alloca…

Yes that is what I mean by allocation being handled by the compiler.
Post reply on HN