繁体   English   中英

C ++到Ada的指针和异常

[英]C++ to Ada with pointers and exceptions

我正在面试潜在的软件工程候选人,并希望将这段C ++代码翻译成Ada:

#include <iostream>

int main(int argc, char *argv[])
{
  int *W = NULL;
  try { *W = 3; }
  catch (...) { std::cout << "default exception"; }
}

与C ++代码一样,我希望Ada代码导致“被信号SIGSEGV终止”。

顺便说一句,我正在使用最新的GNAT(GNAT 9.1.1 20190503(Red Hat 9.1.1-1))

Ada中的相应代码将类似于

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;

但是它不会触发SIGSEGV信号,并且您可以按预期方式收到消息。 此外,编译器已经警告您:

sigsegv.adb:8:04: warning: null value not allowed here
sigsegv.adb:8:04: warning: "Constraint_Error" will be raised at run time

因此,我不确定您能否获得与使用Ada代码的C ++相同的行为...除了从Ada调用C ++之外:D

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转换为Standard.Storage_Error

我已经使用C ++多年,直到最近才与Ada一起工作,我相信,如果选择使用Ada编写的航空电子设备或使用C ++编写的航空电子设备的飞机,我会跳上Ada飞机。 我已经看到C ++随着新标准的每次发布而变得越来越复杂,因此也变得越来越难以管理。

Ada很简单(与C ++相比),因此更易于管理。 我不确定与Ada相比,C ++有哪些优势? 没有一个是显而易见的答案。

国防部在首次引入Ada时完全管理不当真是可惜。 如果他们只是付钱给Borland来开发“ Turbo Ada”,然后将其赠与。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM