comp.lang.ada
 help / color / mirror / Atom feed
* CONSTRAINT ERROR? access check failed
@ 2018-03-01  0:27 Mehdi Saada
  2018-03-01  8:07 ` Niklas Holsti
  2018-03-01  8:30 ` Simon Wright
  0 siblings, 2 replies; 17+ messages in thread
From: Mehdi Saada @ 2018-03-01  0:27 UTC (permalink / raw)


I got
raised CONSTRAINT_ERROR : ledit.adb:34 access check failed
for the "while" line before. I found an old post about this problem, allocated composite object being constrained even when being of an unconstrained subtype... but I hardly understood anything. It's clearly out of my league, and it's not like I don't LOVE to dig and learn, believe me:
  procedure INSERT_OR_REPLACE
     (NUMBER : in LINE_NUMBER; LINE : in STRING) is
      TEMP : access LINK := HEAD;
   begin
      while TEMP.NEXT /= null and TEMP.NEXT.NUM < NUMBER loop
	 TEMP := TEMP.NEXT;
      end loop;
      TEMP.next := new LINK'
           (Num => NUMBER,
	   Length => LINE'Length,
	   NEXT   => (if TEMP.NEXT.NUM = NUMBER then TEMP.NEXT.NEXT else TEMP.NEXT),
	   Line   => LINE);
   end;

So, if someone could provide me a quick solution that don't change much my current code (I already have trouble understanding it, changing the data structure would crush me)... Here's the LINK type and HEAD constant:
type Link (Num : LINE_NUMBER := 0; Length : NATURAL := 0) is
      record
	 NEXT : access LINK := null;
	 Line : String (1 .. Length) := (1..Length => ' ');
      end record;
HEAD   : constant access Link := new LINK '(0,0,null,"");

Instead of a snippet, you could advise me to put assertions here and there to help me see the solution by myself, eventually. But I definitely won't eat the whole damn allocator AARM section...

Mehdi


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: CONSTRAINT ERROR? access check failed
  2018-03-01  0:27 CONSTRAINT ERROR? access check failed Mehdi Saada
@ 2018-03-01  8:07 ` Niklas Holsti
  2018-03-01 12:44   ` Mehdi Saada
  2018-03-01 14:06   ` Mehdi Saada
  2018-03-01  8:30 ` Simon Wright
  1 sibling, 2 replies; 17+ messages in thread
From: Niklas Holsti @ 2018-03-01  8:07 UTC (permalink / raw)


On 18-03-01 02:27 , Mehdi Saada wrote:
> I got
> raised CONSTRAINT_ERROR : ledit.adb:34 access check failed
    ...
>       while TEMP.NEXT /= null and TEMP.NEXT.NUM < NUMBER loop

Use "and then", otherwise the above evaluates TEMP.NEXT.NUM even when 
TEMP.NEXT is null.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: CONSTRAINT ERROR? access check failed
  2018-03-01  0:27 CONSTRAINT ERROR? access check failed Mehdi Saada
  2018-03-01  8:07 ` Niklas Holsti
@ 2018-03-01  8:30 ` Simon Wright
  2018-03-01 12:14   ` Mehdi Saada
  1 sibling, 1 reply; 17+ messages in thread
From: Simon Wright @ 2018-03-01  8:30 UTC (permalink / raw)


Mehdi Saada <00120260a@gmail.com> writes:

>       while TEMP.NEXT /= null and TEMP.NEXT.NUM < NUMBER loop

I think you mean "and then" (as it is, both sides of the "and" are
evaluated).

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: CONSTRAINT ERROR? access check failed
  2018-03-01  8:30 ` Simon Wright
@ 2018-03-01 12:14   ` Mehdi Saada
  2018-03-01 13:27     ` Simon Wright
  0 siblings, 1 reply; 17+ messages in thread
From: Mehdi Saada @ 2018-03-01 12:14 UTC (permalink / raw)


I saw that. That was one of the problem. I worked my ass, and corrected everything during the night. I didn't sleep well, but I'm pretty glad of the end result.

I'm too used to forums, that's why I remove posts when I find myself an answer, or find it irrelevant at one point. Since the post doesn't disappear for everyone (or does it ?), I should stop. I supposes It just does make people click on the title, to see nothing... how irritating. I swear I'll stop it.


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: CONSTRAINT ERROR? access check failed
  2018-03-01  8:07 ` Niklas Holsti
@ 2018-03-01 12:44   ` Mehdi Saada
  2018-03-01 13:45     ` Björn Lundin
  2018-03-01 14:06   ` Mehdi Saada
  1 sibling, 1 reply; 17+ messages in thread
From: Mehdi Saada @ 2018-03-01 12:44 UTC (permalink / raw)


Those are the two last things I would like you people to review. First, I can't get out of that loop, even with the exit INNER; after "Rentrez vos commandes". It starts it again.

INNER : loop
declare
   NOM_FICHIER : String := Get_Line;
   INDICE      : NATURAL := 0;
   begin
	if Index_Non_Blank (NOM_FICHIER) /= 0
	then
	    if not Exists (NOM_FICHIER) then Put_Line ("N'existe pas. Recommencez."); end if;
	    OPEN (FILE_ENTREE, In_File, NOM_FICHIER);
	    Set_Input (FILE_ENTREE);
	    while not End_Of_File loop
		INDICE := INDICE + 10;
		INSERT_OR_REPLACE (INDICE, LET_ONLY_GRAPHIC_CHARACTERS(GET_LINE));
	    end loop;
        end if;
	Put_Line ("Rentrer maintenant vos commandes"); exit INNER;
    exception when others => Put_Line ("ERREUR");
    end;
end loop INNER;

Then, could you tell what you think of that subprogram that is meant to purge a STRING from non graphic characters (those that make the program crash) ?

 function LET_ONLY_GRAPHIC_CHARACTERS
     (INPUT_STRING : in STRING) return STRING is
      use Ada.Strings.Maps.Constants,
	  Ada.Strings.Maps, Ada.Strings.Fixed, Ada.Strings;
      FIRST, I, COUNT, LAST : NATURal := INPUT_STRING'FIRST;
      OUTPUT_STRING         : STRING (INPUT_STRING'Range);

   begin
      loop
	 FIND_TOKEN (Source => INPUT_STRING (I .. INPUT_STRING'LAST),
	      Set    => Graphic_Set,
	      Test   => Inside ,
	      First  => First,
	      Last   => Last);
	 if Last in INPUT_STRING'Range and LAST /= 0 then
	    OUTPUT_STRING (I .. I + LAST - FIRST) := INPUT_STRING (FIRST .. LAST);
	    I := I + LAST - FIRST + 1;
	 else
	    exit; end if;
      end loop;
      PRAGMA Assert (Index (OUTPUT_STRING(OUTPUT_STRING'First .. 
			    (if I > OUTPUT_STRING'Length then OUTPUT_STRING'Last else I))
			      , GRAPHIC_SET, TEST => Outside) = 0, "CHARACTÈRES NON GRAPHIQUE ONT ECHAPPÉS !");
      return OUTPUT_STRING (OUTPUT_STRING'First .. (if I > OUTPUT_STRING'Length then OUTPUT_STRING'Last else I));
   end;


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: CONSTRAINT ERROR? access check failed
  2018-03-01 12:14   ` Mehdi Saada
@ 2018-03-01 13:27     ` Simon Wright
  2018-03-01 13:44       ` Mehdi Saada
  0 siblings, 1 reply; 17+ messages in thread
From: Simon Wright @ 2018-03-01 13:27 UTC (permalink / raw)


Mehdi Saada <00120260a@gmail.com> writes:

> I'm too used to forums, that's why I remove posts when I find myself
> an answer, or find it irrelevant at one point. Since the post doesn't
> disappear for everyone (or does it ?), I should stop. I supposes It
> just does make people click on the title, to see nothing... how
> irritating. I swear I'll stop it.

This isn't a forum, it's a newsgroup. And even on a forum, does removing
the question remove the answer?

On StackOverflow, on the other hand, I'm pretty sure it does (at least
to people with < 10,000 reputation). Personally I am annoyed when a
question which I've spent time answering gets deleted.


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: CONSTRAINT ERROR? access check failed
  2018-03-01 13:27     ` Simon Wright
@ 2018-03-01 13:44       ` Mehdi Saada
  0 siblings, 0 replies; 17+ messages in thread
From: Mehdi Saada @ 2018-03-01 13:44 UTC (permalink / raw)


I'm not used to newsgroup yet. I understand, that's why I did this only if no answers had been posted, and very little time had passed. Answers are not removed, and since answers are often much more interesting than my original questions, it doesn minimal harm. Bad habit.


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: CONSTRAINT ERROR? access check failed
  2018-03-01 12:44   ` Mehdi Saada
@ 2018-03-01 13:45     ` Björn Lundin
  0 siblings, 0 replies; 17+ messages in thread
From: Björn Lundin @ 2018-03-01 13:45 UTC (permalink / raw)


On 2018-03-01 13:44, Mehdi Saada wrote:
> Those are the two last things I would like you people to review. First, I can't get out of that loop, even with the exit INNER; after "Rentrez vos commandes". It starts it again.
> 

seems strange, since you do exit INNNER.
Are you sure that not something else is happening,
like all this is in a procedure that you call again?


> INNER : loop
...
> 	Put_Line ("Rentrer maintenant vos commandes"); exit INNER;
>     exception when others => Put_Line ("ERREUR");
>     end;
> end loop INNER;
> 
> Then, could you tell what you think of that subprogram that is meant to purge a STRING from non graphic characters (those that make the program crash) ?


 I'm likely gettting bashed for using unbounded string like this,
 but I'd do like this

function Let_Only_Graphic_Characters (Input_String : in String) return
String is
  use Ada.Strings.Unbounded;
  use Ada.Characters.Handling;
  Tmp : Unbounded_String;
begin
  for i in Input_String'range loop
    if Is_Graphic(Input_String(i)) then
      Append(Tmp, Input_String(i));
    end if;
  end loop;
  return To_String(Tmp);
end Let_Only_Graphic_Characters;

or with 'for of' loop

function Let_Only_Graphic_Characters (Input_String : in String) return
String is
  use Ada.Strings.Unbounded;
  use Ada.Characters.Handling;
  Tmp : Unbounded_String;
begin
  for C of Input_String loop
    if Is_Graphic(C) then
      Append(Tmp, C);
    end if;
  end loop;
  return To_String(Tmp);
end Let_Only_Graphic_Characters;



-- 
--
Björn

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: CONSTRAINT ERROR? access check failed
  2018-03-01  8:07 ` Niklas Holsti
  2018-03-01 12:44   ` Mehdi Saada
@ 2018-03-01 14:06   ` Mehdi Saada
  2018-03-01 14:31     ` Mehdi Saada
  2018-03-01 14:38     ` Mehdi Saada
  1 sibling, 2 replies; 17+ messages in thread
From: Mehdi Saada @ 2018-03-01 14:06 UTC (permalink / raw)


I won't bash you for that, I thought of it, but I re-discovered the GET_LINE function returning a fixed string, and read about "automatic variables", which I already used without knowing the name, and I try to stick with these constructs, such as:
PUT_LINE("Fill in the name file");
loop
declare
FILL_IN: STRING := TRIM(BOTH, GET_LINE);
begin
if FILL_IN /= "" then exit; end if
exception when others => PUT_LINE("Try again");
end;
end loop;

which are really illegant. I wouldn't be surprised if they were indeed used in the implementation of GET_LINE for UNBOUNDED_STRING.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: CONSTRAINT ERROR? access check failed
  2018-03-01 14:06   ` Mehdi Saada
@ 2018-03-01 14:31     ` Mehdi Saada
  2018-03-01 14:38     ` Mehdi Saada
  1 sibling, 0 replies; 17+ messages in thread
From: Mehdi Saada @ 2018-03-01 14:31 UTC (permalink / raw)


Now I understand: it was part of a bigger loop, and once nothing was entered as a command, the whole loop was walked through, and the file asked again. I just had to put INNER before the main loop. Why can't I see that immediately...
Thanks for your contribution, though.

When entering a file name, it raised raised ADA.IO_EXCEPTIONS.END_ERROR : a-tigeli.adb:96
No exception handler seem to catch that exception. Yet it happens only when I provided an existing file name.
The (rewritten) loop;
loop
      declare
	 NOM_FICHIER : String := Get_Line;
	 INDICE      : NATURAL := 0;
      begin
	 if Index_Non_Blank (NOM_FICHIER) /= 0 then
	    if not Exists (NOM_FICHIER) then Put_Line ("N'existe pas. Recommencez.");
	    else OPEN (FILE_ENTREE, In_File, NOM_FICHIER);
	       Set_Input (FILE_ENTREE);
	       begin
		  while not End_Of_File loop
		     INDICE := INDICE + 10;
		     INSERT_OR_REPLACE (INDICE, LET_ONLY_GRAPHIC_CHARACTERS (GET_LINE));
		  end loop;
		  exit;
	       exception when others => PUT_LINE ("HERE !");
	       end;
	    end if; 
	 end if; end;
   end loop;

I put exception handlers EVERYWHERE, in every function called, besides standard libraries' ones, of course.


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: CONSTRAINT ERROR? access check failed
  2018-03-01 14:06   ` Mehdi Saada
  2018-03-01 14:31     ` Mehdi Saada
@ 2018-03-01 14:38     ` Mehdi Saada
  2018-03-01 15:53       ` Anh Vo
                         ` (2 more replies)
  1 sibling, 3 replies; 17+ messages in thread
From: Mehdi Saada @ 2018-03-01 14:38 UTC (permalink / raw)


Work finished ! Thank you.
But I would like to know why I get a warning at compile time with
type Link (Num : LINE_NUMBER := 0; Length : NATURAL := 0) is
      record
	 NEXT : access LINK := null;
	 Line : String (1 .. Length) := (1..Length => ' ');
      end record;
As if it could cause a stack overflow (was written as is). Yet I don't get at each compilation, surprisingly.


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: CONSTRAINT ERROR? access check failed
  2018-03-01 14:38     ` Mehdi Saada
@ 2018-03-01 15:53       ` Anh Vo
  2018-03-01 17:24         ` Mehdi Saada
  2018-03-01 17:26       ` Simon Wright
  2018-03-01 17:56       ` Jeffrey R. Carter
  2 siblings, 1 reply; 17+ messages in thread
From: Anh Vo @ 2018-03-01 15:53 UTC (permalink / raw)


On Thursday, March 1, 2018 at 6:38:48 AM UTC-8, Mehdi Saada wrote:
> Work finished ! Thank you.
> But I would like to know why I get a warning at compile time with
> type Link (Num : LINE_NUMBER := 0; Length : NATURAL := 0) is
>       record
> 	 NEXT : access LINK := null;
> 	 Line : String (1 .. Length) := (1..Length => ' ');
>       end record;
> As if it could cause a stack overflow (was written as is). Yet I don't get at each compilation, surprisingly.

I would prefer any compiler which gives warning when this code is compiled because Num is declared and assigned but not used.

Anh Vo 


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: CONSTRAINT ERROR? access check failed
  2018-03-01 15:53       ` Anh Vo
@ 2018-03-01 17:24         ` Mehdi Saada
  2018-03-01 18:34           ` Anh Vo
  0 siblings, 1 reply; 17+ messages in thread
From: Mehdi Saada @ 2018-03-01 17:24 UTC (permalink / raw)


> I would prefer any compiler which gives warning when this code is compiled because Num is declared and assigned but not used.
But it is, later, in subprograms. It is read to walked through the linked list.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: CONSTRAINT ERROR? access check failed
  2018-03-01 14:38     ` Mehdi Saada
  2018-03-01 15:53       ` Anh Vo
@ 2018-03-01 17:26       ` Simon Wright
  2018-03-01 17:47         ` Mehdi Saada
  2018-03-01 17:56       ` Jeffrey R. Carter
  2 siblings, 1 reply; 17+ messages in thread
From: Simon Wright @ 2018-03-01 17:26 UTC (permalink / raw)


Mehdi Saada <00120260a@gmail.com> writes:

> Work finished ! Thank you.
> But I would like to know why I get a warning at compile time with
> type Link (Num : LINE_NUMBER := 0; Length : NATURAL := 0) is
>       record
> 	 NEXT : access LINK := null;
> 	 Line : String (1 .. Length) := (1..Length => ' ');
>       end record;
> As if it could cause a stack overflow (was written as is). Yet I don't get at each compilation, surprisingly.

Because Length could be Natural'Last which would make Line large enough
to break the stack on any machine likely to be available to you.

Do you really need the default values? If you have defaults, then
creating an object of the type without specifying values for the
discriminants makes the discriminants changeable (by whole-record
assignment), so that sufficient space has to be reserved for the largest
possible record. Without defaults, you have to supply discriminants when
you create an object (the discriminants then can't be changed, but will
that matter?).


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: CONSTRAINT ERROR? access check failed
  2018-03-01 17:26       ` Simon Wright
@ 2018-03-01 17:47         ` Mehdi Saada
  0 siblings, 0 replies; 17+ messages in thread
From: Mehdi Saada @ 2018-03-01 17:47 UTC (permalink / raw)


You're right, using both dynamic allocation and mutable records is overkill. Having to use the first, negates the need for the others. In my case at least.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: CONSTRAINT ERROR? access check failed
  2018-03-01 14:38     ` Mehdi Saada
  2018-03-01 15:53       ` Anh Vo
  2018-03-01 17:26       ` Simon Wright
@ 2018-03-01 17:56       ` Jeffrey R. Carter
  2 siblings, 0 replies; 17+ messages in thread
From: Jeffrey R. Carter @ 2018-03-01 17:56 UTC (permalink / raw)


On 03/01/2018 03:38 PM, Mehdi Saada wrote:
> Work finished ! Thank you.
> But I would like to know why I get a warning at compile time with
> type Link (Num : LINE_NUMBER := 0; Length : NATURAL := 0) is
>        record
> 	 NEXT : access LINK := null;
> 	 Line : String (1 .. Length) := (1..Length => ' ');
>        end record;
> As if it could cause a stack overflow (was written as is). Yet I don't get at each compilation, surprisingly.

Because you have defaults for your discriminants, you can declare

V : Link;

V is then unconstrained. You can change its discriminants through whole-record 
assignment:

V := (Num => 23, Length => 42, Next => null, Line => (1 .. 42 => 'S') );

The way GNAT handles this is to allocate space for the largest variant, and only 
use the part for the current discriminant. Apparently GNAT doesn't use a stack 
large enough to handle a string of length Integer'Last.

Why is Num a discriminant? There's no reason for a discriminant that isn't used 
in the record definition.

-- 
Jeff Carter
"Facts do not cease to exist because they are ignored."
Aldous Huxley
134

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: CONSTRAINT ERROR? access check failed
  2018-03-01 17:24         ` Mehdi Saada
@ 2018-03-01 18:34           ` Anh Vo
  0 siblings, 0 replies; 17+ messages in thread
From: Anh Vo @ 2018-03-01 18:34 UTC (permalink / raw)


On Thursday, March 1, 2018 at 9:24:23 AM UTC-8, Mehdi Saada wrote:
> > I would prefer any compiler which gives warning when this code is compiled because Num is declared and assigned but not used.
> But it is, later, in subprograms. It is read to walked through the linked list.

Then, it should be declared outside of the type.

AV

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2018-03-01 18:34 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-01  0:27 CONSTRAINT ERROR? access check failed Mehdi Saada
2018-03-01  8:07 ` Niklas Holsti
2018-03-01 12:44   ` Mehdi Saada
2018-03-01 13:45     ` Björn Lundin
2018-03-01 14:06   ` Mehdi Saada
2018-03-01 14:31     ` Mehdi Saada
2018-03-01 14:38     ` Mehdi Saada
2018-03-01 15:53       ` Anh Vo
2018-03-01 17:24         ` Mehdi Saada
2018-03-01 18:34           ` Anh Vo
2018-03-01 17:26       ` Simon Wright
2018-03-01 17:47         ` Mehdi Saada
2018-03-01 17:56       ` Jeffrey R. Carter
2018-03-01  8:30 ` Simon Wright
2018-03-01 12:14   ` Mehdi Saada
2018-03-01 13:27     ` Simon Wright
2018-03-01 13:44       ` Mehdi Saada

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox