comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: ADA.STRINGS.INDEX_ERROR : a-strunb.adb:782
Date: Mon, 15 Jan 2018 05:23:30 +0200
Date: 2018-01-15T05:23:30+02:00	[thread overview]
Message-ID: <fc2l5iFoaa9U1@mid.individual.net> (raw)
In-Reply-To: <d66b4a89-dc02-4c04-a5bf-6dd4f2ef9f86@googlegroups.com>

On 18-01-15 03:18 , Mehdi Saada wrote:
> This if statement exactly, seems to raise
> "ADA.STRINGS.INDEX_ERROR : a-strunb.adb:782"(No indication of line, so I can't know more).
> What does this exception means ?

That you have tried to access a non-existent element of some kind of 
string (Bounded or Unbounded). Note that Ada.Strings.Bounded and 
Ada.Strings.Unbounded are both children of Ada.Strings, where this 
exception is declared, and both use this exception.

> You can see there's no string type variable to be accessed, here.
> CHAINE is of UNBOUNDED_STRING type.
> LA_PILE is a stack of characters,
> EMPILER means "putting on the stack's top" in french.
>
> if IS_BASIC(ELEMENT(CHAINE,IND)) then

The exception probably comes from the above call of ELEMENT.

>    EMPILER(LA_PILE,ELEMENT(CHAINE,IND));
> else DELETE(CHAINE, IND,IND);
> end if;
>
> It's enclosed in:
> for Ind in 1..Length(CHAINE) loop

Note that the iteration range, 1 .. Length(CHAINE), is computed here 
using the value of CHAINE at the *start* of the loop, and is not 
recomputed during the execution of the loop.

>         begin
>             if IS_BASIC(ELEMENT(CHAINE,IND)) then
>                     EMPILER(LA_PILE,ELEMENT(CHAINE,IND));
>             else DELETE(CHAINE, IND,IND);

This DELETE shortens CHAINE. If it occurs one or more times during the 
iteration of the loop, before the last iteration of the loop, then at 
some later iteration the index Ind will be larger than the (new) 
Length(CHAINE), and the exception results.

>             end if;
>          end;
> end loop;

To fix this error, you must make the loop recompute Length(CHAINE) when 
needed, for example in this way:

    for Ind in 1 .. Length(CHAINE) loop

       exit when Ind > Length(CHAINE);

       if IS_BASIC ...
          ...
       end if;

    end loop;

However, I would write it like this, to avoid the confusion of a double 
check of Length(CHAINE):

    Ind : Positive := 1;
    ...

    while Ind <= Length(CHAINE) loop

       if IS_BASIC ...
          ...
       end if;

       Ind := Ind + 1;

    end loop;

Note that in the "while" loop, the condition (Ind <= Length(CHAINE)) is 
evaluated on every loop iteration. In a "for" loop, the index range is 
evaluated once, at the start of the loop.

However, I suspect that there is a deeper error in your loop, related to 
the DELETE. Consider what happens if CHAINE is, say, "ABCDEFG", and the 
DELETE occurs for the first time when Ind = 3, at CHAINE(Ind) = 'C'. 
When you DELETE(CHAINE, 3, 3), CHAINE becomes "ABDEFG". On the next loop 
iteration, Ind = 4, which means the loop checks CHAINE(4) = 'E'. The 
letter 'D', which used to be CHAINE(4) but is now CHAINE(3), is not checked.

Do you really need to DELETE in the loop? If you do, then you must take 
into account that DELETE renumbers the elements of CHAINE after the 
deleted character.

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


  reply	other threads:[~2018-01-15  3:23 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-15  1:18 ADA.STRINGS.INDEX_ERROR : a-strunb.adb:782 Mehdi Saada
2018-01-15  3:23 ` Niklas Holsti [this message]
2018-01-15  8:29 ` Dmitry A. Kazakov
2018-01-15 10:49   ` Simon Wright
2018-01-15 11:51     ` Mehdi Saada
2018-01-15 12:00       ` Mehdi Saada
2018-01-15 13:32         ` Dmitry A. Kazakov
2018-01-15 18:55         ` Shark8
2018-01-15 13:51       ` Simon Wright
2018-01-15 15:18         ` Mehdi Saada
2018-01-15 15:29           ` Mehdi Saada
2018-01-15 15:50             ` Mehdi Saada
2018-01-15 17:53               ` Simon Wright
2018-01-15 17:22   ` Anh Vo
2018-01-15 17:48     ` Dmitry A. Kazakov
2018-01-15 19:56       ` Anh Vo
2018-01-16 16:06     ` Brian Drummond
2018-01-16 17:21       ` Anh Vo
2018-01-15 10:37 ` Simon Wright
2018-01-15 16:06 ` Mehdi Saada
2018-01-17 19:23   ` Niklas Holsti
2018-01-16 16:24 ` Mehdi Saada
2018-01-16 18:20   ` Jacob Sparre Andersen
2018-01-16 20:00     ` Mehdi Saada
2018-01-17 15:45 ` Mehdi Saada
2018-01-17 16:30   ` Dmitry A. Kazakov
2018-01-17 22:50     ` Mehdi Saada
2018-01-18  8:34       ` Dmitry A. Kazakov
2018-01-18 11:14         ` Simon Wright
2018-01-18 11:55           ` Dmitry A. Kazakov
2018-01-18 12:21             ` Mehdi Saada
2018-01-18 13:08               ` J-P. Rosen
2018-01-18 15:23                 ` Dmitry A. Kazakov
2018-01-19  1:10                   ` Randy Brukardt
2018-01-19  8:52                     ` Dmitry A. Kazakov
2018-01-20  0:42                       ` Randy Brukardt
2018-01-20  9:47                         ` Dmitry A. Kazakov
2018-01-23  0:44                           ` Randy Brukardt
2018-01-18 14:05         ` AdaMagica
2018-01-18 14:17           ` Mehdi Saada
2018-01-18 14:56             ` J-P. Rosen
2018-01-18 15:38               ` Mehdi Saada
2018-01-19  1:13                 ` Randy Brukardt
2018-01-18 16:48             ` AdaMagica
2018-01-18 16:59               ` Dmitry A. Kazakov
2018-01-18 19:36                 ` Mehdi Saada
2018-01-19  1:21                   ` Randy Brukardt
2018-01-19 16:15                 ` AdaMagica
2018-01-19 16:58                   ` Dmitry A. Kazakov
2018-01-21 23:27             ` G.B.
replies disabled

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