From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,ed1e3e7e7d67edd4,start X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!217.73.144.45.MISMATCH!feeder2.ecngs.de!ecngs!feeder.ecngs.de!130.59.10.21.MISMATCH!kanaga.switch.ch!switch.ch!news.grnet.gr!newsfd02.forthnet.gr!not-for-mail From: Christos Chryssochoidis Newsgroups: comp.lang.ada Subject: Newbie's question Date: Mon, 11 Feb 2008 16:31:34 +0200 Organization: FORTHnet S.A., Atthidon 4, GR-17671 Kalithea, Greece, Tel: +30 2109559000, Fax: +30 2109559333, url: http://www.forthnet.gr Message-ID: <1202740198.391371@athprx03> NNTP-Posting-Host: athprx03.forthnet.gr Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: athprx02.forthnet.gr 1202740294 9425 193.92.150.68 (11 Feb 2008 14:31:34 GMT) X-Complaints-To: abuse@forthnet.gr NNTP-Posting-Date: Mon, 11 Feb 2008 14:31:34 +0000 (UTC) User-Agent: Thunderbird 2.0.0.9 (Macintosh/20071031) Cache-Post-Path: newsfd02!unknown@adsl54-133.ath.forthnet.gr Xref: g2news1.google.com comp.lang.ada:19757 Date: 2008-02-11T16:31:34+02:00 List-Id: Hello, I 'm experimenting with Ada, and I'm having trouble getting some toy code running. The code is the following: (hope it isn't too complicated) -- This is a program to test passing subprograms as arguments to other subprograms. -- It consists of a main program, "Test_Filter", and two nested subprograms: "Filter" and "Greater_Equal_3". The "Filter" subprogram takes as arguments an array of Integers and a function (predicate), and returns an array of the same type, having only those elements of the input array that satisfy the given predicate. For some reason that I can't figure out, I'm getting a "segmentation fault" when I run the executable. (It compiles fine.) with Ada.Containers.Doubly_Linked_Lists; with Ada.Text_IO; procedure Test_Filter is subtype Index is Natural; type Int_Array is array(Index range<>) of Integer; function Filter(Elements : Int_Array; Predicate : not null access function(Element : Integer) return Boolean) return Int_Array is package Int_Lists is new Ada.Containers.Doubly_Linked_Lists(Element_Type => Integer); Tmp_List : Int_Lists.List; Result : access Int_Array; begin for I in Elements'Range loop if Predicate(Elements(I)) then Tmp_List.Append(Elements(I)); end if; end loop; Result := new Int_Array(1..Index(Tmp_List.Length)); Copy_List: declare Tmp_List_Cursor : Int_Lists.Cursor := Tmp_List.First; I : Integer := 1; begin while Int_Lists.Has_Element(Tmp_List_Cursor) loop Result(I) := Int_Lists.Element(Tmp_List_Cursor); Tmp_List_Cursor := Int_Lists.Next(Tmp_List_Cursor); I := I + 1; end loop; end Copy_List; return Result.all; end Filter; function Greater_Equal_3(Element :Integer) return Boolean is begin if Element >= 3 then return True; else return False; end if; end Greater_Equal_3; Int_Array1 : Int_Array := Int_Array'(1,2,3,4,5); Int_Array2 : Int_Array(1..3); begin Int_Array2 := Filter(Int_Array1, Greater_Equal_3'Access); for I in Int_Array1'Range loop Ada.Text_IO.Put_Line(Int_Array1(I)'Img); end loop; Ada.Text_IO.Put_Line(""); for I in Int_Array2'Range loop Ada.Text_IO.Put_Line(Int_Array2(I)'Img); end loop; end Test_Filter; Thanks very much for any help.