Board index » delphi » Dynamic array + invalid pointer operation

Dynamic array + invalid pointer operation

The code below (compiled with D4) raises an EInvalidPointer exception
('invalid pointer operation'). It works fine if I set the max value of
Count or the length of the dynamic array to a smaller value.
Does anybody know the reason for this and what to do?

  PROCEDURE TForm1.Button1Click;
    VAR List  : TList;
        Item  : ^Integer;
        DynArr: ARRAY OF Boolean;
        Count : Integer;
  BEGIN
    List:=TList.Create;
    TRY
      FOR Count:=1 TO 5 DO
        BEGIN
          New(Item);
          Item^:=Count;
          List.Add(Item);
        END;
      SetLength(DynArr,8);
      FOR Count:=1 TO 8 DO
        DynArr[Count]:= (Count=1) OR (Count=2) OR (Count=6);
    FINALLY
      ...

Best regards
Tom

Sent via Deja.com http://www.deja.com/
Before you buy.

 

Re:Dynamic array + invalid pointer operation


Quote
"Thomas Bucks" <thomas_bu...@my-deja.com> wrote in message

news:8oe62n$j0t$1@nnrp1.deja.com...

Quote
> The code below (compiled with D4) raises an EInvalidPointer exception
> ('invalid pointer operation'). It works fine if I set the max value of
> Count or the length of the dynamic array to a smaller value.
> Does anybody know the reason for this and what to do?

>   PROCEDURE TForm1.Button1Click;
>     VAR List  : TList;
>         Item  : ^Integer;
>         DynArr: ARRAY OF Boolean;
>         Count : Integer;
>   BEGIN
>     List:=TList.Create;
>     TRY
>       FOR Count:=1 TO 5 DO
>         BEGIN
>           New(Item);
>           Item^:=Count;
>           List.Add(Item);
>         END;
>       SetLength(DynArr,8);
>       FOR Count:=1 TO 8 DO
>         DynArr[Count]:= (Count=1) OR (Count=2) OR (Count=6);
>     FINALLY

Dynamic arrays are indexed from zero, not 1.

Re:Dynamic array + invalid pointer operation


Dynamic arrays indices start from 0. In your examples you can refer to
cells 0-7 but not 8.

Quote
"Thomas Bucks" <thomas_bu...@my-deja.com> wrote in message

news:8oe62n$j0t$1@nnrp1.deja.com...
Quote
> The code below (compiled with D4) raises an EInvalidPointer exception
> ('invalid pointer operation'). It works fine if I set the max value of
> Count or the length of the dynamic array to a smaller value.
> Does anybody know the reason for this and what to do?

>   PROCEDURE TForm1.Button1Click;
>     VAR List  : TList;
>         Item  : ^Integer;
>         DynArr: ARRAY OF Boolean;
>         Count : Integer;
>   BEGIN
>     List:=TList.Create;
>     TRY
>       FOR Count:=1 TO 5 DO
>         BEGIN
>           New(Item);
>           Item^:=Count;
>           List.Add(Item);
>         END;
>       SetLength(DynArr,8);
>       FOR Count:=1 TO 8 DO
>         DynArr[Count]:= (Count=1) OR (Count=2) OR (Count=6);
>     FINALLY
>       ...

> Best regards
> Tom

> Sent via Deja.com http://www.deja.com/
> Before you buy.

Other Threads