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-Received: by 2002:ac8:4555:: with SMTP id z21mr19658448qtn.284.1591545239625; Sun, 07 Jun 2020 08:53:59 -0700 (PDT) X-Received: by 2002:aca:4e0d:: with SMTP id c13mr7935336oib.30.1591545239345; Sun, 07 Jun 2020 08:53:59 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!aioe.org!peer02.ams4!peer.am4.highwinds-media.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sun, 7 Jun 2020 08:53:59 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: google-groups.googlegroups.com; posting-host=67.164.24.162; posting-account=Qh2kiQoAAADpCLlhT_KTYoGO8dU3n4I6 NNTP-Posting-Host: 67.164.24.162 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <7ba8ec59-de9c-4d8a-96ad-1d5c85d3995bo@googlegroups.com> Subject: Re: CONSTRAINT ERROR: erroneous memory access From: Anh Vo Injection-Date: Sun, 07 Jun 2020 15:53:59 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Received-Bytes: 8965 X-Received-Body-CRC: 1251898758 Xref: reader01.eternal-september.org comp.lang.ada:59009 Date: 2020-06-07T08:53:59-07:00 List-Id: On Saturday, June 6, 2020 at 4:40:04 PM UTC-7, jcu...@gmail.com wrote: > It has been a few years since I have written Ada; I used and taught Ada 9= 5 back when I was working for a defense contractor. But, now that I'm retir= ed, I wanted to get up to speed with Ada 2012, so I decided to implement a = main program to see how the Shortest_Paths generic package works (taken fro= m A.18.31, pages 574-576). But, when I read in the data and call the Shorte= st_Path function, it returns with CONSTRAINT ERROR: erroneous memory access= . I've tried multiple times, but can't seem to figure out what I'm doing (o= r NOT doing) wrong.=20 >=20 > Here's the main program: >=20 > with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; > with Ada.Float_Text_IO; use Ada.Float_Text_IO; > with Ada.Text_IO; use Ada.Text_IO; > with Shortest_Paths; > with Ada.Command_Line; use Ada.Command_Line; > with DirectedEdge; use DirectedEdge; > with Ada.Containers; use Ada.Containers; > with Ada.Exceptions; >=20 > procedure Main_Test is >=20 > Input_File : Ada.Text_IO.File_Type; >=20 > Vertices : Integer; -- Number of nodes > Edges : Integer; -- Number of paths >=20 > Tail : Integer; -- Node From > Head : Integer; -- Node To > Weight : Float; -- Path Weight/Distance/Cost >=20 > -- Instantiate Shortest Paths package with 0..Integer'Last subtype > package SP is new Shortest_Paths(Node =3D> Natural); > =20 > -- Use Edge'Read to read the Edge components into Item > =20 > -- Display directed edge components > procedure Display(Edge : in SP.Edge) is > begin > Put(Edge.From, Width=3D>1); Put("->"); > Put(Edge.To, Width=3D>1); Put(" "); > Put(Float(Edge.Length), Fore =3D> 0, Aft =3D> 2, Exp =3D> 0); Put("= "); > end Display; >=20 > -- Display directed edge components at cursor > -- Replace List'Write with Display > procedure Display(Cursor: in SP.Adjacency_Lists.Cursor) > is > Edge : SP.Edge :=3D SP.Adjacency_Lists.Element(Cursor); > begin > Display(Edge); -- Let other procedure do all the work > end Display; >=20 > begin >=20 > -- Open input file using arg 1 > Open (File =3D> Input_File, > Mode =3D> In_File, > Name =3D> Argument(1)); -- ../tinyEWD.txt >=20 > Set_Input(Input_File); -- Redirect input > New_Line; > Put("Processing '"); Put(Argument(1)); Put_Line("'"); >=20 > -- Read number of nodes (vertices) > Get(Vertices); New_Line; > Put("Vertices: ");Put(Vertices, width=3D>2);New_Line; >=20 > -- Read number of paths (edges) > Get(Edges); > Put("Edges: ");Put(Edges, Width=3D>2);New_Line(2); >=20 > declare > =20 > -- Constrain Vertex to zero-based subrange > subtype Vertex is Natural range 0..Vertices-1; > =20 > -- Adj is DLL of Adjacency Lists for each Vertex > Adj : array (Vertex) of SP.Adjacency_Lists.List; > =20 > -- Edge is a record of Tail, Head, and Weight components > Edge : SP.Edge; > =20 > begin > =20 > Put_Line("Creating Adjacency Lists"); New_Line; > =20 > -- For each node, create empty list of adjacent nodes > Create_Empty_Adjacency_Lists: for Node in Vertex loop > =20 > -- Initialize each adjacency list to empty > Adj(Node) :=3D SP.Adjacency_Lists.Empty_List; > =20 > end loop Create_Empty_Adjacency_Lists; > =20 > -- Display and append new edge to adjacency list for node > -- Constrain Edge index to natural subrange > Append_New_Edge: for E in 0..Edges-1 loop > =20 > Put("Edge: ");Put(E, Width=3D>2);Put(" "); > =20 > -- Get edge components from data file > Get(Tail); -- Tail > Get(Head); -- Head > Get(Weight); -- Distance/Weight > =20 > -- Set edge components > Edge.From :=3D Tail; > Edge.To :=3D Head; > Edge.Length :=3D SP.Distance(Weight); > =20 > -- Display Edge > Display(Edge); > Put(" Appended to edge ");Put(Tail,1); > New_Line; > =20 > -- Append new edge to From adjacency list > -- Indicating path to another node > Adj(Edge.From).Append(Edge); > =20 > end loop Append_New_Edge; > New_Line; > =20 > Close(Input_File); > Set_Input(Standard_Input); -- Reset input source > =20 > Put_Line("Node Adjacency Lists"); > =20 > -- Display contents of each adjacency list > Display_Adjacency_Lists: for Node in Vertex loop > =20 > Put("Adj[");Put(Node, Width=3D>1);Put("] "); > =20 > declare > Edges : Ada.Containers.Count_Type; > begin > =20 > -- How many edges are in this node? > Edges :=3D SP.Adjacency_Lists.Length(Adj(Node)); > Put(Integer(Edges), Width =3D> 1); > if (Edges > 1) then > Put(" Edges: "); > else > Put(" Edge: "); > end if; > =20 > -- Iterate over all nodes in this adjacency list > -- and Display each edge for each node > SP.Adjacency_Lists.Iterate(Adj(Node), Process =3D> Display'Ac= cess); > =20 > end; > New_Line; > =20 > end loop Display_Adjacency_Lists; > New_Line; > =20 > -- Create Edge-Weighted Graph of Node and Adjacency List > declare > EWG : SP.Graphs.Vector; -- Edge Weighted Graphs > Path : SP.Paths.List; -- Shortest Path > begin > =20 > Put_Line("Creating Edge-Weighted Graphs."); > EWG :=3D SP.Graphs.Empty_Vector; > Put_Line("EWG Empty_Vector created."); > =20 > Put("Initializing Shortest Path to empty list..."); > Path :=3D SP.Paths.Empty_List; > Put_Line("done."); > =20 > for Vertex in 0..Vertices-1 loop > Put("Vertex: ");Put(Vertex, Width =3D> 1); > EWG.Append(Adj(Vertex)); > Put_Line(" appended to EWG."); > end loop; > New_Line; > =20 > Put("EWG.Length =3D "); Put(Integer(EWG.Length), Width =3D> 1);N= ew_Line; > =20 > Put_Line("Finding shortest path from node 0 to node 6"); > declare > use Ada.Exceptions; > begin > -- Compute Shortest Path > Path :=3D SP.Shortest_Path > (EWG, Source =3D> 0, Target =3D> 6); > exception > when Error: others =3D> > New_Line; > Put_Line(Exception_Name(Error)); > Put_Line(Exception_Message(Error)); > New_Line; > Return; > end; > =20 > Put("The Shortest Path from Node 0 to Node 6 "); > Put("contains "); Put(Integer(Path.Length)); Put(" entries."); > -- Display path > end; > =20 > =20 > end; >=20 > end Main_Test; >=20 > And here's the test data (taken from Algorithms, by Sedwick): >=20 > 8 > 15 > 4 5 0.35 > 5 4 0.35 > 4 7 0.37 > 5 7 0.28 > 7 5 0.28 > 5 1 0.32 > 0 4 0.38 > 0 2 0.26 > 7 3 0.39 > 1 3 0.29 > 2 7 0.34 > 6 2 0.40 > 3 6 0.52 > 6 0 0.58 > 6 4 0.93 It is hard to comment without Ada units Shortest_Path and DirectedEdge post= ed. In other word, your code does not compile. Anh Vo=20