Reputation: 715
I'm interviewing potential software engineering candidates and would like a translation into Ada of this piece of C++ code:
#include <iostream>
int main(int argc, char *argv[])
{
int *W = NULL;
try { *W = 3; }
catch (...) { std::cout << "default exception"; }
}
As with the C++ code, I want the Ada code to result in "terminated by signal SIGSEGV".
BTW, I'm using the latest GNAT (GNAT 9.1.1 20190503 (Red Hat 9.1.1-1) )
Upvotes: 0
Views: 384
Reputation: 715
Having spent years working with C++ and only just recently Ada, I believe if given a choice of a plane with its avionics written in Ada or one with its avionics written C++, I'd jump on the Ada plane. I've watched C++ grow with every release of a new standard ever more complex and, consequently, ever more unmanageable.
Ada is simple (compared to C++) and hence, much easier to manage. I'm not sure what C++ advantages exist vs Ada? None is the obvious answer.
It's a shame the DoD completely mismanaged Ada when it was first introduced. If they'd only paid Borland to develop a "Turbo Ada" and then given it away.
Upvotes: 1
Reputation: 706
with Ada.Wide_Wide_Text_IO; use Ada.Wide_Wide_Text_IO;
with System;
procedure SigSegV is
W : Integer with Import, Address => System.Null_Address;
begin
W := 3;
exception
when others =>
Put_Line ("default exception");
end SigSegV;
SIGSEGV is converted to Standard.Storage_Error
Upvotes: 2
Reputation: 1641
The corresponding code in Ada would be something like
with Ada.Text_IO; use Ada.Text_IO;
procedure SigSegV is
type Int_Ptr is access Integer;
W : Int_Ptr := null;
begin
W.all := 3;
exception
when others =>
Put_Line ("default exception");
end SigSegV;
But it doesn't trigger a SIGSEGV signal and you get the message as expected. Moreover, the compiler already warned you :
sigsegv.adb:8:04: warning: null value not allowed here
sigsegv.adb:8:04: warning: "Constraint_Error" will be raised at run time
So I am not sure that you can get the same behaviour than in C++ with Ada code... Apart from calling C++ from Ada :D
Upvotes: 6