指向性メモ::2008-04-14

ページ情報
制作日
2008-04-14T17:08:07+09:00
最終更新日
2008-04-14T17:08:07+09:00

Adaならそんなことにはならなかった

Created:
2008-04-14T17:08:07+09:00

以前話題になっていたこの記事であるが、Adaの場合Segmentation Faultが起こる前に言語側で制約違反が起こるので、例外処理がきちんと出来ていれば即死を免れることが出来る。

procedure Pointer is
   N :access Integer;
begin
   begin
      N.all := 5;
   exception
      when Constraint_Error =>
         -- do something
   end;
end Pointer;

いや、そもそもAdaならnot null accessがあるので、このようなコードを書いてしまう状況になるかどうかも疑わしい。

procedure Pointer is
   N : not null access Integer := new Integer'(5);
begin
   -- do something
end Pointer;

この場合、宣言部で初期化をしないとコンパイラがwarningを吐くので間違いに気がつくことが出来る。

もちろん、毎回not nullを書くのは面倒であるし、もしかしたら書き忘れてしまうかもしれない。そういう場合は型自体を定義してしまえばいい。

procedure Pointer is
   type Integer_Access is not null access Integer;
   N : Integer_Access := new Integer'(5);
begin
   -- do something
end Pointer
;

これで安心である。

Comments
0
Trackbacks
0
PermaLink
http://yudai.arielworks.com/memo/2008/04/14/170807
連絡先、リンク、転載や複製などについては『サイト案内』をご覧ください。Powered by HIMMEL

I ♥ Validator